--account
CREATE OR REPLACE FUNCTION sp_account_insert(
	_username	character varying(20),
	_password	character varying(20) DEFAULT null,
	_status	integer DEFAULT null,
	_email	character varying(256) DEFAULT null,
	_type	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO account(username,password,status,email,type)
	VALUES(_username,_password,_status,_email,_type);
RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_account_select(_username	character varying(20))
RETURNS SETOF account
AS $$
BEGIN
	RETURN QUERY SELECT * FROM account WHERE username = _username;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_account_selectList (
	_limit	integer,
	_offset integer,
	_username	character varying(20) DEFAULT null,
	_password	character varying(20) DEFAULT null,
	_status	integer DEFAULT null,
	_email	character varying(256) DEFAULT null,
	_type	integer DEFAULT null
)
RETURNS SETOF account
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM account WHERE
	 	(_username is null OR username = _username)	
	AND (_password is null OR password = _password)	
	AND (_status is null OR status = _status)	
	AND (_email is null OR email = _email)	
	AND (_type is null OR type = _type)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_account_count (
	_username	character varying(20) DEFAULT null,
	_password	character varying(20) DEFAULT null,
	_status	integer DEFAULT null,
	_email	character varying(256) DEFAULT null,
	_type	integer DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM account WHERE
	 	(_username is null OR username = _username)	
	AND (_password is null OR password = _password)	
	AND (_status is null OR status = _status)	
	AND (_email is null OR email = _email)	
	AND (_type is null OR type = _type);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_account_delete(_username character varying(20))
	RETURNS INT as
$$
BEGIN
		DELETE FROM account
 		WHERE "username" = _username;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_account_update 
(	_username character varying(20),
	_password character varying(20),
	_status integer,
	_email character varying(256),
	_type integer  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE account 
	SET 		password = _password,
		status = _status,
		email = _email,
		type = _type
 	WHERE username = _username;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;
--end of account

-- chat
CREATE OR REPLACE FUNCTION sp_chat_insert(
	_sender	character varying(20),
	_receiver	character varying(20),
	_message	character varying(256),
	_sent_date	timestamp without time zone
)RETURNS int AS $$
BEGIN
	INSERT INTO chat(sender,receiver,message,sent_date)
	VALUES(_sender,_receiver,_message,_sent_date);
RETURN currval('chat_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_chat_select(_id	integer)
RETURNS SETOF chat
AS $$
BEGIN
	RETURN QUERY SELECT * FROM chat WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_chat_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_sender	character varying(20) DEFAULT null,
	_receiver	character varying(20) DEFAULT null,
	_message	character varying(256) DEFAULT null,
	_sent_dateFrom	timestamp without time zone DEFAULT null,
	_sent_dateTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF chat
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM chat WHERE
	 	(_id is null OR id = _id)	
	AND (_sender is null OR sender = _sender)	
	AND (_receiver is null OR receiver = _receiver)	
	AND (_message is null OR message = _message)	
	AND (_sent_dateFrom is null OR sent_date >= _sent_dateFrom)
	AND	(_sent_dateTo is null OR sent_date <= _sent_dateTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_chat_count (
	_id	integer DEFAULT null,
	_sender	character varying(20) DEFAULT null,
	_receiver	character varying(20) DEFAULT null,
	_message	character varying(256) DEFAULT null,
	_sent_dateFrom	timestamp without time zone DEFAULT null,
	_sent_dateTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM chat WHERE
	 	(_id is null OR id = _id)	
	AND (_sender is null OR sender = _sender)	
	AND (_receiver is null OR receiver = _receiver)	
	AND (_message is null OR message = _message)	
	AND (_sent_dateFrom is null OR sent_date >= _sent_dateFrom)
	AND	(_sent_dateTo is null OR sent_date <= _sent_dateTo);
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION sp_chat_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM chat
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_chat_update 
(	_id integer,
	_sender character varying(20),
	_receiver character varying(20),
	_message character varying(256),
	_sent_date timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE chat 
	SET 		sender = _sender,
		receiver = _receiver,
		message = _message,
		sent_date = _sent_date
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;
-- end of chat

--activity
CREATE OR REPLACE FUNCTION sp_activity_insert(
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_type	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_id_person	integer DEFAULT null,
	_activity_date	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO activity(name,code,description,type,id_company,id_person,activity_date,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_name,_code,_description,_type,_id_company,_id_person,_activity_date,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('activity_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_activity_select(_id	integer)
RETURNS SETOF activity
AS $$
BEGIN
	RETURN QUERY SELECT * FROM activity WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_activity_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_type	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_id_person	integer DEFAULT null,
	_activity_dateFrom	timestamp without time zone DEFAULT null,
	_activity_dateTo	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF activity
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM activity WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_type is null OR type = _type)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_id_person is null OR id_person = _id_person)	
	AND (_activity_dateFrom is null OR activity_date >= _activity_dateFrom)
	AND	(_activity_dateTo is null OR activity_date <= _activity_dateTo)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_activity_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_type	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_id_person	integer DEFAULT null,
	_activity_dateFrom	timestamp without time zone DEFAULT null,
	_activity_dateTo	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM activity WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_type is null OR type = _type)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_id_person is null OR id_person = _id_person)	
	AND (_activity_dateFrom is null OR activity_date >= _activity_dateFrom)
	AND	(_activity_dateTo is null OR activity_date <= _activity_dateTo)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_activity_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM activity
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_activity_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_type integer,
	_id_company integer,
	_id_person integer,
	_activity_date timestamp without time zone,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE activity 
	SET 		name = _name,
		code = _code,
		description = _description,
		type = _type,
		id_company = _id_company,
		id_person = _id_person,
		activity_date = _activity_date,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;
-- end of activity

--activity_attendee
CREATE OR REPLACE FUNCTION sp_activity_attendee_insert(
	_id_activity	integer DEFAULT null,
	_is_employee	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_person	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO activity_attendee(id_activity,is_employee,id_employee,id_person,note,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_activity,_is_employee,_id_employee,_id_person,_note,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('activity_attendee_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_activity_attendee_select(_id	integer)
RETURNS SETOF activity_attendee
AS $$
BEGIN
	RETURN QUERY SELECT * FROM activity_attendee WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_activity_attendee_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_activity	integer DEFAULT null,
	_is_employee	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_person	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF activity_attendee
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM activity_attendee WHERE
	 	(_id is null OR id = _id)	
	AND (_id_activity is null OR id_activity = _id_activity)	
	AND (_is_employee is null OR is_employee = _is_employee)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_person is null OR id_person = _id_person)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_activity_attendee_count (
	_id	integer DEFAULT null,
	_id_activity	integer DEFAULT null,
	_is_employee	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_person	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM activity_attendee WHERE
	 	(_id is null OR id = _id)	
	AND (_id_activity is null OR id_activity = _id_activity)	
	AND (_is_employee is null OR is_employee = _is_employee)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_person is null OR id_person = _id_person)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_activity_attendee_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM activity_attendee
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_activity_attendee_update 
(	_id integer,
	_id_activity integer,
	_is_employee integer,
	_id_employee integer,
	_id_person integer,
	_note character varying,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE activity_attendee 
	SET 		id_activity = _id_activity,
		is_employee = _is_employee,
		id_employee = _id_employee,
		id_person = _id_person,
		note = _note,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;
-- end of activity_attendee

-- agenda
CREATE OR REPLACE FUNCTION sp_agenda_insert(
	_id_project	integer,
	_id_event	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_author	integer DEFAULT null,
	_id_approver	integer DEFAULT null,
	_start_time	timestamp without time zone DEFAULT null,
	_id_venue	integer DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO agenda(id_project,id_event,name,code,description,id_author,id_approver,start_time,id_venue,status,notification,creator,updator,deletion_time)
	VALUES(_id_project,_id_event,_name,_code,_description,_id_author,_id_approver,_start_time,_id_venue,_status,_notification,_creator,_creator,null);
RETURN currval('agenda_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_agenda_select(_id	integer)
RETURNS SETOF agenda
AS $$
BEGIN
	RETURN QUERY SELECT * FROM agenda WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_agenda_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_id_event	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_author	integer DEFAULT null,
	_id_approver	integer DEFAULT null,
	_start_timeFrom	timestamp without time zone DEFAULT null,
	_start_timeTo	timestamp without time zone DEFAULT null,
	_id_venue	integer DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF agenda
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM agenda WHERE
	 	(_id is null OR id = _id)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_id_event is null OR id_event = _id_event)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_author is null OR id_author = _id_author)	
	AND (_id_approver is null OR id_approver = _id_approver)	
	AND (_start_timeFrom is null OR start_time >= _start_timeFrom)
	AND	(_start_timeTo is null OR start_time <= _start_timeTo)	
	AND (_id_venue is null OR id_venue = _id_venue)	
	AND (_status is null OR status = _status)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_agenda_count (
	_id	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_id_event	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_author	integer DEFAULT null,
	_id_approver	integer DEFAULT null,
	_start_timeFrom	timestamp without time zone DEFAULT null,
	_start_timeTo	timestamp without time zone DEFAULT null,
	_id_venue	integer DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM agenda WHERE
	 	(_id is null OR id = _id)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_id_event is null OR id_event = _id_event)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_author is null OR id_author = _id_author)	
	AND (_id_approver is null OR id_approver = _id_approver)	
	AND (_start_timeFrom is null OR start_time >= _start_timeFrom)
	AND	(_start_timeTo is null OR start_time <= _start_timeTo)	
	AND (_id_venue is null OR id_venue = _id_venue)	
	AND (_status is null OR status = _status)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_agenda_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM agenda
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_agenda_update 
(	_id integer,
	_id_project integer,
	_id_event integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_id_author integer,
	_id_approver integer,
	_start_time timestamp without time zone,
	_id_venue integer,
	_status integer,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE agenda 
	SET 		id_project = _id_project,
		id_event = _id_event,
		name = _name,
		code = _code,
		description = _description,
		id_author = _id_author,
		id_approver = _id_approver,
		start_time = _start_time,
		id_venue = _id_venue,
		status = _status,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of agenda

-- agenda_item
CREATE OR REPLACE FUNCTION sp_agenda_item_insert(
	_id_agenda	integer,
	_id_parent	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_description	character varying DEFAULT null,
	_remark	character varying DEFAULT null,
	_is_group	boolean DEFAULT null,
	_id_prev_item	integer DEFAULT null,
	_start_time	timestamp without time zone DEFAULT null,
	_duration	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_sound	character varying DEFAULT null,
	_light	character varying DEFAULT null,
	_left_screen	character varying DEFAULT null,
	_right_screen	character varying DEFAULT null,
	_center_screen	character varying DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_id_type	integer DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO agenda_item(id_agenda,id_parent,name,description,remark,is_group,id_prev_item,start_time,duration,id_employee,id_company,sound,light,left_screen,right_screen,center_screen,code,id_type,status,notification,creator,updator,deletion_time)
	VALUES(_id_agenda,_id_parent,_name,_description,_remark,_is_group,_id_prev_item,_start_time,_duration,_id_employee,_id_company,_sound,_light,_left_screen,_right_screen,_center_screen,_code,_id_type,_status,_notification,_creator,_creator,null);
RETURN currval('agenda_item_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_agenda_item_select(_id	integer)
RETURNS SETOF agenda_item
AS $$
BEGIN
	RETURN QUERY SELECT * FROM agenda_item WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_agenda_item_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_agenda	integer DEFAULT null,
	_id_parent	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_description	character varying DEFAULT null,
	_remark	character varying DEFAULT null,
	_is_group	boolean DEFAULT null,
	_id_prev_item	integer DEFAULT null,
	_start_timeFrom	timestamp without time zone DEFAULT null,
	_start_timeTo	timestamp without time zone DEFAULT null,
	_duration	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_sound	character varying DEFAULT null,
	_light	character varying DEFAULT null,
	_left_screen	character varying DEFAULT null,
	_right_screen	character varying DEFAULT null,
	_center_screen	character varying DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_id_type	integer DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF agenda_item
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM agenda_item WHERE
	 	(_id is null OR id = _id)	
	AND (_id_agenda is null OR id_agenda = _id_agenda)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_name is null OR name = _name)	
	AND (_description is null OR description = _description)	
	AND (_remark is null OR remark = _remark)	
	AND (_is_group is null OR is_group = _is_group)	
	AND (_id_prev_item is null OR id_prev_item = _id_prev_item)	
	AND (_start_timeFrom is null OR start_time >= _start_timeFrom)
	AND	(_start_timeTo is null OR start_time <= _start_timeTo)	
	AND (_duration is null OR duration = _duration)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_sound is null OR sound = _sound)	
	AND (_light is null OR light = _light)	
	AND (_left_screen is null OR left_screen = _left_screen)	
	AND (_right_screen is null OR right_screen = _right_screen)	
	AND (_center_screen is null OR center_screen = _center_screen)	
	AND (_code is null OR code = _code)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_status is null OR status = _status)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_agenda_item_count (
	_id	integer DEFAULT null,
	_id_agenda	integer DEFAULT null,
	_id_parent	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_description	character varying DEFAULT null,
	_remark	character varying DEFAULT null,
	_is_group	boolean DEFAULT null,
	_id_prev_item	integer DEFAULT null,
	_start_timeFrom	timestamp without time zone DEFAULT null,
	_start_timeTo	timestamp without time zone DEFAULT null,
	_duration	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_sound	character varying DEFAULT null,
	_light	character varying DEFAULT null,
	_left_screen	character varying DEFAULT null,
	_right_screen	character varying DEFAULT null,
	_center_screen	character varying DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_id_type	integer DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM agenda_item WHERE
	 	(_id is null OR id = _id)	
	AND (_id_agenda is null OR id_agenda = _id_agenda)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_name is null OR name = _name)	
	AND (_description is null OR description = _description)	
	AND (_remark is null OR remark = _remark)	
	AND (_is_group is null OR is_group = _is_group)	
	AND (_id_prev_item is null OR id_prev_item = _id_prev_item)	
	AND (_start_timeFrom is null OR start_time >= _start_timeFrom)
	AND	(_start_timeTo is null OR start_time <= _start_timeTo)	
	AND (_duration is null OR duration = _duration)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_sound is null OR sound = _sound)	
	AND (_light is null OR light = _light)	
	AND (_left_screen is null OR left_screen = _left_screen)	
	AND (_right_screen is null OR right_screen = _right_screen)	
	AND (_center_screen is null OR center_screen = _center_screen)	
	AND (_code is null OR code = _code)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_status is null OR status = _status)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_agenda_item_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM agenda_item
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_agenda_item_update 
(	_id integer,
	_id_agenda integer,
	_id_parent integer,
	_name character varying(255),
	_description character varying,
	_remark character varying,
	_is_group boolean,
	_id_prev_item integer,
	_start_time timestamp without time zone,
	_duration integer,
	_id_employee integer,
	_id_company integer,
	_sound character varying,
	_light character varying,
	_left_screen character varying,
	_right_screen character varying,
	_center_screen character varying,
	_code character varying(20),
	_id_type integer,
	_status integer,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE agenda_item 
	SET 		id_agenda = _id_agenda,
		id_parent = _id_parent,
		name = _name,
		description = _description,
		remark = _remark,
		is_group = _is_group,
		id_prev_item = _id_prev_item,
		start_time = _start_time,
		duration = _duration,
		id_employee = _id_employee,
		id_company = _id_company,
		sound = _sound,
		light = _light,
		left_screen = _left_screen,
		right_screen = _right_screen,
		center_screen = _center_screen,
		code = _code,
		id_type = _id_type,
		status = _status,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of agenda_item

-- allocation
CREATE OR REPLACE FUNCTION sp_allocation_insert(
	_note	character varying DEFAULT null,
	_id_role	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_phase	integer DEFAULT null,
	_work_day	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO allocation(note,id_role,id_employee,id_phase,work_day,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_note,_id_role,_id_employee,_id_phase,_work_day,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('allocation_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_allocation_select(_id	integer)
RETURNS SETOF allocation
AS $$
BEGIN
	RETURN QUERY SELECT * FROM allocation WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_allocation_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_id_role	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_phase	integer DEFAULT null,
	_work_day	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF allocation
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM allocation WHERE
	 	(_id is null OR id = _id)	
	AND (_note is null OR note = _note)	
	AND (_id_role is null OR id_role = _id_role)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_phase is null OR id_phase = _id_phase)	
	AND (_work_day is null OR work_day = _work_day)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_allocation_count (
	_id	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_id_role	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_phase	integer DEFAULT null,
	_work_day	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM allocation WHERE
	 	(_id is null OR id = _id)	
	AND (_note is null OR note = _note)	
	AND (_id_role is null OR id_role = _id_role)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_phase is null OR id_phase = _id_phase)	
	AND (_work_day is null OR work_day = _work_day)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_allocation_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM allocation
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_allocation_update 
(	_id integer,
	_note character varying,
	_id_role integer,
	_id_employee integer,
	_id_phase integer,
	_work_day integer,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE allocation 
	SET 		note = _note,
		id_role = _id_role,
		id_employee = _id_employee,
		id_phase = _id_phase,
		work_day = _work_day,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of allocation

-- approval
CREATE OR REPLACE FUNCTION sp_approval_insert(
	_object_type	character varying(50) DEFAULT null,
	_object_id	character varying(50) DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_prev_approval	integer DEFAULT null,
	_id_next_approval	integer DEFAULT null,
	_asking_time	timestamp without time zone DEFAULT null,
	_confirm_time	timestamp without time zone DEFAULT null,
	_title	character varying(255) DEFAULT null,
	_description	character varying(511) DEFAULT null,
	_note	character varying(511) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO approval(object_type,object_id,id_employee,id_prev_approval,id_next_approval,asking_time,confirm_time,title,description,note,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_object_type,_object_id,_id_employee,_id_prev_approval,_id_next_approval,_asking_time,_confirm_time,_title,_description,_note,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('approval_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_approval_select(_id	integer)
RETURNS SETOF approval
AS $$
BEGIN
	RETURN QUERY SELECT * FROM approval WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_approval_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	character varying(50) DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_prev_approval	integer DEFAULT null,
	_id_next_approval	integer DEFAULT null,
	_asking_timeFrom	timestamp without time zone DEFAULT null,
	_asking_timeTo	timestamp without time zone DEFAULT null,
	_confirm_timeFrom	timestamp without time zone DEFAULT null,
	_confirm_timeTo	timestamp without time zone DEFAULT null,
	_title	character varying(255) DEFAULT null,
	_description	character varying(511) DEFAULT null,
	_note	character varying(511) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF approval
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM approval WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_prev_approval is null OR id_prev_approval = _id_prev_approval)	
	AND (_id_next_approval is null OR id_next_approval = _id_next_approval)	
	AND (_asking_timeFrom is null OR asking_time >= _asking_timeFrom)
	AND	(_asking_timeTo is null OR asking_time <= _asking_timeTo)	
	AND (_confirm_timeFrom is null OR confirm_time >= _confirm_timeFrom)
	AND	(_confirm_timeTo is null OR confirm_time <= _confirm_timeTo)	
	AND (_title is null OR title = _title)	
	AND (_description is null OR description = _description)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_approval_count (
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	character varying(50) DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_prev_approval	integer DEFAULT null,
	_id_next_approval	integer DEFAULT null,
	_asking_timeFrom	timestamp without time zone DEFAULT null,
	_asking_timeTo	timestamp without time zone DEFAULT null,
	_confirm_timeFrom	timestamp without time zone DEFAULT null,
	_confirm_timeTo	timestamp without time zone DEFAULT null,
	_title	character varying(255) DEFAULT null,
	_description	character varying(511) DEFAULT null,
	_note	character varying(511) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM approval WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_prev_approval is null OR id_prev_approval = _id_prev_approval)	
	AND (_id_next_approval is null OR id_next_approval = _id_next_approval)	
	AND (_asking_timeFrom is null OR asking_time >= _asking_timeFrom)
	AND	(_asking_timeTo is null OR asking_time <= _asking_timeTo)	
	AND (_confirm_timeFrom is null OR confirm_time >= _confirm_timeFrom)
	AND	(_confirm_timeTo is null OR confirm_time <= _confirm_timeTo)	
	AND (_title is null OR title = _title)	
	AND (_description is null OR description = _description)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_approval_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM approval
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_approval_update 
(	_id integer,
	_object_type character varying(50),
	_object_id character varying(50),
	_id_employee integer,
	_id_prev_approval integer,
	_id_next_approval integer,
	_asking_time timestamp without time zone,
	_confirm_time timestamp without time zone,
	_title character varying(255),
	_description character varying(511),
	_note character varying(511),
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE approval 
	SET 		object_type = _object_type,
		object_id = _object_id,
		id_employee = _id_employee,
		id_prev_approval = _id_prev_approval,
		id_next_approval = _id_next_approval,
		asking_time = _asking_time,
		confirm_time = _confirm_time,
		title = _title,
		description = _description,
		note = _note,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of approval

-- attendee
CREATE OR REPLACE FUNCTION sp_attendee_insert(
	_id_event	integer,
	_id_attendee_type	integer,
	_number	integer,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO attendee(id_event,id_attendee_type,number,created,creator,updated,updator,deletion_time)
	VALUES(_id_event,_id_attendee_type,_number,_created,_creator,_updated,_creator,null);
RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_attendee_select(_id_event	integer , _id_attendee_type	integer)
RETURNS SETOF attendee
AS $$
BEGIN
	RETURN QUERY SELECT * FROM attendee WHERE id_event = _id_event AND id_attendee_type = _id_attendee_type;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_attendee_selectList (
	_limit	integer,
	_offset integer,
	_id_event	integer DEFAULT null,
	_id_attendee_type	integer DEFAULT null,
	_number	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF attendee
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM attendee WHERE
	 	(_id_event is null OR id_event = _id_event)	
	AND (_id_attendee_type is null OR id_attendee_type = _id_attendee_type)	
	AND (_number is null OR number = _number)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_attendee_count (
	_id_event	integer DEFAULT null,
	_id_attendee_type	integer DEFAULT null,
	_number	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM attendee WHERE
	 	(_id_event is null OR id_event = _id_event)	
	AND (_id_attendee_type is null OR id_attendee_type = _id_attendee_type)	
	AND (_number is null OR number = _number)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_attendee_delete(_id_event integer,	_id_attendee_type integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM attendee
 		WHERE "id_event" = _id_event AND "id_attendee_type" = _id_attendee_type;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_attendee_update 
(	_id_event integer,
	_id_attendee_type integer,
	_number integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE attendee 
	SET 		number = _number,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id_event = _id_event and id_attendee_type = _id_attendee_type;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of attendee

-- attendee_type
CREATE OR REPLACE FUNCTION sp_attendee_type_insert(
	_name	character varying(255),
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO attendee_type(name,code,description,created,creator,updated,updator,deletion_time)
	VALUES(_name,_code,_description,_created,_creator,_updated,_creator,null);
RETURN currval('attendee_type_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_attendee_type_select(_id	integer)
RETURNS SETOF attendee_type
AS $$
BEGIN
	RETURN QUERY SELECT * FROM attendee_type WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_attendee_type_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF attendee_type
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM attendee_type WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_attendee_type_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM attendee_type WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_attendee_type_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM attendee_type
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_attendee_type_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE attendee_type 
	SET 		name = _name,
		code = _code,
		description = _description,
		updated = now() ,
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of attendee_type

-- brain_storm
CREATE OR REPLACE FUNCTION sp_brain_storm_insert(
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_title	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_edit_mode	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_width	integer DEFAULT null,
	_height	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO brain_storm(object_type,object_id,title,code,description,edit_mode,id_employee,width,height,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_object_type,_object_id,_title,_code,_description,_edit_mode,_id_employee,_width,_height,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('brain_storm_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_brain_storm_select(_id	integer)
RETURNS SETOF brain_storm
AS $$
BEGIN
	RETURN QUERY SELECT * FROM brain_storm WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_brain_storm_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_title	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_edit_mode	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_width	integer DEFAULT null,
	_height	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF brain_storm
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM brain_storm WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_title is null OR title = _title)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_edit_mode is null OR edit_mode = _edit_mode)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_width is null OR width = _width)	
	AND (_height is null OR height = _height)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_brain_storm_count (
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_title	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_edit_mode	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_width	integer DEFAULT null,
	_height	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM brain_storm WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_title is null OR title = _title)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_edit_mode is null OR edit_mode = _edit_mode)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_width is null OR width = _width)	
	AND (_height is null OR height = _height)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_brain_storm_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM brain_storm
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_brain_storm_update 
(	_id integer,
	_object_type character varying(50),
	_object_id integer,
	_title character varying(255),
	_code character varying(20),
	_description character varying,
	_edit_mode integer,
	_id_employee integer,
	_width integer,
	_height integer,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE brain_storm 
	SET 		object_type = _object_type,
		object_id = _object_id,
		title = _title,
		code = _code,
		description = _description,
		edit_mode = _edit_mode,
		id_employee = _id_employee,
		width = _width,
		height = _height,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of brain_storm

-- brain_storm_node
CREATE OR REPLACE FUNCTION sp_brain_storm_node_insert(
	_title	character varying(255) DEFAULT null,
	_type	integer DEFAULT null,
	_content	character varying DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_parent	integer DEFAULT null,
	_x	integer DEFAULT null,
	_y	integer DEFAULT null,
	_style	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO brain_storm_node(title,type,content,object_type,object_id,id_employee,id_parent,x,y,style,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_title,_type,_content,_object_type,_object_id,_id_employee,_id_parent,_x,_y,_style,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('brain_storm_node_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_brain_storm_node_select(_id	integer)
RETURNS SETOF brain_storm_node
AS $$
BEGIN
	RETURN QUERY SELECT * FROM brain_storm_node WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_brain_storm_node_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_title	character varying(255) DEFAULT null,
	_type	integer DEFAULT null,
	_content	character varying DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_parent	integer DEFAULT null,
	_x	integer DEFAULT null,
	_y	integer DEFAULT null,
	_style	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF brain_storm_node
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM brain_storm_node WHERE
	 	(_id is null OR id = _id)	
	AND (_title is null OR title = _title)	
	AND (_type is null OR type = _type)	
	AND (_content is null OR content = _content)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_x is null OR x = _x)	
	AND (_y is null OR y = _y)	
	AND (_style is null OR style = _style)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_brain_storm_node_count (
	_id	integer DEFAULT null,
	_title	character varying(255) DEFAULT null,
	_type	integer DEFAULT null,
	_content	character varying DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_parent	integer DEFAULT null,
	_x	integer DEFAULT null,
	_y	integer DEFAULT null,
	_style	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM brain_storm_node WHERE
	 	(_id is null OR id = _id)	
	AND (_title is null OR title = _title)	
	AND (_type is null OR type = _type)	
	AND (_content is null OR content = _content)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_x is null OR x = _x)	
	AND (_y is null OR y = _y)	
	AND (_style is null OR style = _style)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_brain_storm_node_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM brain_storm_node
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_brain_storm_node_update 
(	_id integer,
	_title character varying(255),
	_type integer,
	_content character varying,
	_object_type character varying(50),
	_object_id integer,
	_id_employee integer,
	_id_parent integer,
	_x integer,
	_y integer,
	_style character varying,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE brain_storm_node 
	SET 		title = _title,
		type = _type,
		content = _content,
		object_type = _object_type,
		object_id = _object_id,
		id_employee = _id_employee,
		id_parent = _id_parent,
		x = _x,
		y = _y,
		style = _style,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of brain_storm_node

-- brand
CREATE OR REPLACE FUNCTION sp_brand_insert(
	_name	character varying(255),
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO brand(name,code,description,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_name,_code,_description,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('brand_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_brand_select(_id	integer)
RETURNS SETOF brand
AS $$
BEGIN
	RETURN QUERY SELECT * FROM brand WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_brand_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF brand
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM brand WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_brand_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM brand WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_brand_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM brand
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_brand_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE brand 
	SET 		name = _name,
		code = _code,
		description = _description,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of brand

--cash_issue
CREATE OR REPLACE FUNCTION sp_cash_issue_insert(
	_type	integer DEFAULT null,
	_id_receivable	integer DEFAULT null,
	_id_payable	integer DEFAULT null,
	_plan_date	timestamp without time zone DEFAULT null,
	_real_date	timestamp without time zone DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_value	numeric DEFAULT null,
	_need_approval	boolean DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO cash_issue(type,id_receivable,id_payable,plan_date,real_date,name,code,description,value,need_approval,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_type,_id_receivable,_id_payable,_plan_date,_real_date,_name,_code,_description,_value,_need_approval,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('cash_issue_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_cash_issue_select(_id	integer)
RETURNS SETOF cash_issue
AS $$
BEGIN
	RETURN QUERY SELECT * FROM cash_issue WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_cash_issue_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_type	integer DEFAULT null,
	_id_receivable	integer DEFAULT null,
	_id_payable	integer DEFAULT null,
	_plan_dateFrom	timestamp without time zone DEFAULT null,
	_plan_dateTo	timestamp without time zone DEFAULT null,
	_real_dateFrom	timestamp without time zone DEFAULT null,
	_real_dateTo	timestamp without time zone DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_value	numeric DEFAULT null,
	_need_approval	boolean DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF cash_issue
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM cash_issue WHERE
	 	(_id is null OR id = _id)	
	AND (_type is null OR type = _type)	
	AND (_id_receivable is null OR id_receivable = _id_receivable)	
	AND (_id_payable is null OR id_payable = _id_payable)	
	AND (_plan_dateFrom is null OR plan_date >= _plan_dateFrom)
	AND	(_plan_dateTo is null OR plan_date <= _plan_dateTo)	
	AND (_real_dateFrom is null OR real_date >= _real_dateFrom)
	AND	(_real_dateTo is null OR real_date <= _real_dateTo)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_value is null OR value = _value)	
	AND (_need_approval is null OR need_approval = _need_approval)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_cash_issue_count (
	_id	integer DEFAULT null,
	_type	integer DEFAULT null,
	_id_receivable	integer DEFAULT null,
	_id_payable	integer DEFAULT null,
	_plan_dateFrom	timestamp without time zone DEFAULT null,
	_plan_dateTo	timestamp without time zone DEFAULT null,
	_real_dateFrom	timestamp without time zone DEFAULT null,
	_real_dateTo	timestamp without time zone DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_value	numeric DEFAULT null,
	_need_approval	boolean DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM cash_issue WHERE
	 	(_id is null OR id = _id)	
	AND (_type is null OR type = _type)	
	AND (_id_receivable is null OR id_receivable = _id_receivable)	
	AND (_id_payable is null OR id_payable = _id_payable)	
	AND (_plan_dateFrom is null OR plan_date >= _plan_dateFrom)
	AND	(_plan_dateTo is null OR plan_date <= _plan_dateTo)	
	AND (_real_dateFrom is null OR real_date >= _real_dateFrom)
	AND	(_real_dateTo is null OR real_date <= _real_dateTo)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_value is null OR value = _value)	
	AND (_need_approval is null OR need_approval = _need_approval)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_cash_issue_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM cash_issue
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_cash_issue_update 
(	_id integer,
	_type integer,
	_id_receivable integer,
	_id_payable integer,
	_plan_date timestamp without time zone,
	_real_date timestamp without time zone,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_value numeric,
	_need_approval boolean,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE cash_issue 
	SET 		type = _type,
		id_receivable = _id_receivable,
		id_payable = _id_payable,
		plan_date = _plan_date,
		real_date = _real_date,
		name = _name,
		code = _code,
		description = _description,
		value = _value,
		need_approval = _need_approval,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of cash_issue

-- cash_receipt
CREATE OR REPLACE FUNCTION sp_cash_receipt_insert(
	_type	integer DEFAULT null,
	_id_receivable	integer DEFAULT null,
	_id_payable	integer DEFAULT null,
	_plan_date	timestamp without time zone DEFAULT null,
	_real_date	timestamp without time zone DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_value	numeric DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO cash_receipt(type,id_receivable,id_payable,plan_date,real_date,name,code,description,value,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_type,_id_receivable,_id_payable,_plan_date,_real_date,_name,_code,_description,_value,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('cash_receipt_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_cash_receipt_select(_id	integer)
RETURNS SETOF cash_receipt
AS $$
BEGIN
	RETURN QUERY SELECT * FROM cash_receipt WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_cash_receipt_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_type	integer DEFAULT null,
	_id_receivable	integer DEFAULT null,
	_id_payable	integer DEFAULT null,
	_plan_dateFrom	timestamp without time zone DEFAULT null,
	_plan_dateTo	timestamp without time zone DEFAULT null,
	_real_dateFrom	timestamp without time zone DEFAULT null,
	_real_dateTo	timestamp without time zone DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_value	numeric DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF cash_receipt
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM cash_receipt WHERE
	 	(_id is null OR id = _id)	
	AND (_type is null OR type = _type)	
	AND (_id_receivable is null OR id_receivable = _id_receivable)	
	AND (_id_payable is null OR id_payable = _id_payable)	
	AND (_plan_dateFrom is null OR plan_date >= _plan_dateFrom)
	AND	(_plan_dateTo is null OR plan_date <= _plan_dateTo)	
	AND (_real_dateFrom is null OR real_date >= _real_dateFrom)
	AND	(_real_dateTo is null OR real_date <= _real_dateTo)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_value is null OR value = _value)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_cash_receipt_count (
	_id	integer DEFAULT null,
	_type	integer DEFAULT null,
	_id_receivable	integer DEFAULT null,
	_id_payable	integer DEFAULT null,
	_plan_dateFrom	timestamp without time zone DEFAULT null,
	_plan_dateTo	timestamp without time zone DEFAULT null,
	_real_dateFrom	timestamp without time zone DEFAULT null,
	_real_dateTo	timestamp without time zone DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_value	numeric DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM cash_receipt WHERE
	 	(_id is null OR id = _id)	
	AND (_type is null OR type = _type)	
	AND (_id_receivable is null OR id_receivable = _id_receivable)	
	AND (_id_payable is null OR id_payable = _id_payable)	
	AND (_plan_dateFrom is null OR plan_date >= _plan_dateFrom)
	AND	(_plan_dateTo is null OR plan_date <= _plan_dateTo)	
	AND (_real_dateFrom is null OR real_date >= _real_dateFrom)
	AND	(_real_dateTo is null OR real_date <= _real_dateTo)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_value is null OR value = _value)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_cash_receipt_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM cash_receipt
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_cash_receipt_update 
(	_id integer,
	_type integer,
	_id_receivable integer,
	_id_payable integer,
	_plan_date timestamp without time zone,
	_real_date timestamp without time zone,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_value numeric,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE cash_receipt 
	SET 		type = _type,
		id_receivable = _id_receivable,
		id_payable = _id_payable,
		plan_date = _plan_date,
		real_date = _real_date,
		name = _name,
		code = _code,
		description = _description,
		value = _value,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of cash_receipt

-- checklist
CREATE OR REPLACE FUNCTION sp_checklist_insert(
	_id_project	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	character varying(50) DEFAULT null,
	_title	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_type	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_id_author	integer DEFAULT null,
	_id_approver	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO checklist(id_project,object_type,object_id,title,code,description,type,status,is_draft,notification,id_author,id_approver,creator,updator,deletion_time)
	VALUES(_id_project,_object_type,_object_id,_title,_code,_description,_type,_status,_is_draft,_notification,_id_author,_id_approver,_creator,_creator,null);
RETURN currval('checklist_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_checklist_select(_id	integer)
RETURNS SETOF checklist
AS $$
BEGIN
	RETURN QUERY SELECT * FROM checklist WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_checklist_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	character varying(50) DEFAULT null,
	_title	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_type	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_id_author	integer DEFAULT null,
	_id_approver	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF checklist
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM checklist WHERE
	 	(_id is null OR id = _id)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_title is null OR title = _title)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_type is null OR type = _type)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_id_author is null OR id_author = _id_author)	
	AND (_id_approver is null OR id_approver = _id_approver)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_checklist_count (
	_id	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	character varying(50) DEFAULT null,
	_title	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_type	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_id_author	integer DEFAULT null,
	_id_approver	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM checklist WHERE
	 	(_id is null OR id = _id)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_title is null OR title = _title)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_type is null OR type = _type)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_id_author is null OR id_author = _id_author)	
	AND (_id_approver is null OR id_approver = _id_approver)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_checklist_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM checklist
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_checklist_update 
(	_id integer,
	_id_project integer,
	_object_type character varying(50),
	_object_id character varying(50),
	_title character varying(255),
	_code character varying(20),
	_description character varying,
	_type integer,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_id_author integer,
	_id_approver integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE checklist 
	SET 		id_project = _id_project,
		object_type = _object_type,
		object_id = _object_id,
		title = _title,
		code = _code,
		description = _description,
		type = _type,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		id_author = _id_author,
		id_approver = _id_approver,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of checklist

-- checklist_item
CREATE OR REPLACE FUNCTION sp_checklist_item_insert(
	_id_checklist	integer,
	_display_index	character varying(10) DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_remark	character varying DEFAULT null,
	_is_group	boolean DEFAULT null,
	_id_parent	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_start_time	timestamp without time zone DEFAULT null,
	_end_time	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_index	integer DEFAULT null,
	_type	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null,
	_id_phase	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO checklist_item(id_checklist,display_index,name,remark,is_group,id_parent,id_employee,id_company,start_time,end_time,status,index,type,is_draft,notification,creator,updator,deletion_time,id_phase)
	VALUES(_id_checklist,_display_index,_name,_remark,_is_group,_id_parent,_id_employee,_id_company,_start_time,_end_time,_status,_index,_type,_is_draft,_notification,_creator,_creator,null,_id_phase);
RETURN currval('checklist_item_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_checklist_item_select(_id	integer)
RETURNS SETOF checklist_item
AS $$
BEGIN
	RETURN QUERY SELECT * FROM checklist_item WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_checklist_item_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_checklist	integer DEFAULT null,
	_display_index	character varying(10) DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_remark	character varying DEFAULT null,
	_is_group	boolean DEFAULT null,
	_id_parent	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_start_timeFrom	timestamp without time zone DEFAULT null,
	_start_timeTo	timestamp without time zone DEFAULT null,
	_end_timeFrom	timestamp without time zone DEFAULT null,
	_end_timeTo	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_index	integer DEFAULT null,
	_type	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_id_phase	integer DEFAULT null
)
RETURNS SETOF checklist_item
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM checklist_item WHERE
	 	(_id is null OR id = _id)	
	AND (_id_checklist is null OR id_checklist = _id_checklist)	
	AND (_display_index is null OR display_index = _display_index)	
	AND (_name is null OR name = _name)	
	AND (_remark is null OR remark = _remark)	
	AND (_is_group is null OR is_group = _is_group)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_start_timeFrom is null OR start_time >= _start_timeFrom)
	AND	(_start_timeTo is null OR start_time <= _start_timeTo)	
	AND (_end_timeFrom is null OR end_time >= _end_timeFrom)
	AND	(_end_timeTo is null OR end_time <= _end_timeTo)	
	AND (_status is null OR status = _status)	
	AND (_index is null OR index = _index)	
	AND (_type is null OR type = _type)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_id_phase is null OR id_phase = _id_phase)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_checklist_item_count (
	_id	integer DEFAULT null,
	_id_checklist	integer DEFAULT null,
	_display_index	character varying(10) DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_remark	character varying DEFAULT null,
	_is_group	boolean DEFAULT null,
	_id_parent	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_start_timeFrom	timestamp without time zone DEFAULT null,
	_start_timeTo	timestamp without time zone DEFAULT null,
	_end_timeFrom	timestamp without time zone DEFAULT null,
	_end_timeTo	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_index	integer DEFAULT null,
	_type	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_id_phase	integer DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM checklist_item WHERE
	 	(_id is null OR id = _id)	
	AND (_id_checklist is null OR id_checklist = _id_checklist)	
	AND (_display_index is null OR display_index = _display_index)	
	AND (_name is null OR name = _name)	
	AND (_remark is null OR remark = _remark)	
	AND (_is_group is null OR is_group = _is_group)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_start_timeFrom is null OR start_time >= _start_timeFrom)
	AND	(_start_timeTo is null OR start_time <= _start_timeTo)	
	AND (_end_timeFrom is null OR end_time >= _end_timeFrom)
	AND	(_end_timeTo is null OR end_time <= _end_timeTo)	
	AND (_status is null OR status = _status)	
	AND (_index is null OR index = _index)	
	AND (_type is null OR type = _type)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_id_phase is null OR id_phase = _id_phase);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_checklist_item_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM checklist_item
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_checklist_item_update 
(	_id integer,
	_id_checklist integer,
	_display_index character varying(10),
	_name character varying(255),
	_remark character varying,
	_is_group boolean,
	_id_parent integer,
	_id_employee integer,
	_id_company integer,
	_start_time timestamp without time zone,
	_end_time timestamp without time zone,
	_status integer,
	_index integer,
	_type integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone,
	_id_phase integer  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE checklist_item 
	SET 		id_checklist = _id_checklist,
		display_index = _display_index,
		name = _name,
		remark = _remark,
		is_group = _is_group,
		id_parent = _id_parent,
		id_employee = _id_employee,
		id_company = _id_company,
		start_time = _start_time,
		end_time = _end_time,
		status = _status,
		index = _index,
		type = _type,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time,
		id_phase = _id_phase
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;


-- end of checklist_item

-- comment
CREATE OR REPLACE FUNCTION sp_comment_insert(
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_title	character varying(255) DEFAULT null,
	_content	character varying DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_replied_comment	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO comment(object_type,object_id,title,content,id_employee,id_replied_comment,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_object_type,_object_id,_title,_content,_id_employee,_id_replied_comment,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('comment_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_comment_select(_id	integer)
RETURNS SETOF comment
AS $$
BEGIN
	RETURN QUERY SELECT * FROM comment WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_comment_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_title	character varying(255) DEFAULT null,
	_content	character varying DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_replied_comment	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF comment
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM comment WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_title is null OR title = _title)	
	AND (_content is null OR content = _content)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_replied_comment is null OR id_replied_comment = _id_replied_comment)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_comment_count (
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_title	character varying(255) DEFAULT null,
	_content	character varying DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_replied_comment	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM comment WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_title is null OR title = _title)	
	AND (_content is null OR content = _content)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_replied_comment is null OR id_replied_comment = _id_replied_comment)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_comment_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM comment
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_comment_update 
(	_id integer,
	_object_type character varying(50),
	_object_id integer,
	_title character varying(255),
	_content character varying,
	_id_employee integer,
	_id_replied_comment integer,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE comment 
	SET 		object_type = _object_type,
		object_id = _object_id,
		title = _title,
		content = _content,
		id_employee = _id_employee,
		id_replied_comment = _id_replied_comment,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of comment

-- company
CREATE OR REPLACE FUNCTION sp_company_select(_id	integer)
RETURNS SETOF company
AS $$
BEGIN
	RETURN QUERY SELECT * FROM company WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_company_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM company
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_company_insert(
	_code	character varying(20) DEFAULT null,
	_is_competitor	boolean DEFAULT null,
	_abb_name	character varying(10) DEFAULT null,
	_short_name	character varying(25) DEFAULT null,
	_full_name	character varying(255) DEFAULT null,
	_foreign_name	character varying(255) DEFAULT null,
	_address	character varying(255) DEFAULT null,
	_telephone	character varying(20) DEFAULT null,
	_fax	character varying(50) DEFAULT null,
	_tax_code	character varying(25) DEFAULT null,
	_remark	character varying DEFAULT null,
	_contact_person_1	character varying(255) DEFAULT null,
	_contact_phone_1	character varying(50) DEFAULT null,
	_contact_mobile_1	character varying(50) DEFAULT null,
	_contact_email_1	character varying(255) DEFAULT null,
	_contact_person_2	character varying(255) DEFAULT null,
	_contact_phone_2	character varying(50) DEFAULT null,
	_contact_mobile_2	character varying(50) DEFAULT null,
	_contact_email_2	character varying(255) DEFAULT null,
	_id_place	integer DEFAULT null,
	_map_object	oid DEFAULT null,
	_map_file	bytea DEFAULT null,
	_map_path	character varying(255) DEFAULT null,
	_description	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null,
	_is_provider	boolean DEFAULT null,
	_is_client	boolean DEFAULT null,
	_is_broker	boolean DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO company(code,is_competitor,abb_name,short_name,full_name,foreign_name,address,telephone,fax,tax_code,remark,contact_person_1,contact_phone_1,contact_mobile_1,contact_email_1,contact_person_2,contact_phone_2,contact_mobile_2,contact_email_2,id_place,map_object,map_file,map_path,description,status,is_draft,notification,creator,updator,deletion_time,is_provider,is_client,is_broker)
	VALUES(_code,_is_competitor,_abb_name,_short_name,_full_name,_foreign_name,_address,_telephone,_fax,_tax_code,_remark,_contact_person_1,_contact_phone_1,_contact_mobile_1,_contact_email_1,_contact_person_2,_contact_phone_2,_contact_mobile_2,_contact_email_2,_id_place,_map_object,_map_file,_map_path,_description,_status,_is_draft,_notification,_creator,_creator,null,_is_provider,_is_client,_is_broker);
RETURN currval('company_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_company_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_is_competitor	boolean DEFAULT null,
	_abb_name	character varying(10) DEFAULT null,
	_short_name	character varying(25) DEFAULT null,
	_full_name	character varying(255) DEFAULT null,
	_foreign_name	character varying(255) DEFAULT null,
	_address	character varying(255) DEFAULT null,
	_telephone	character varying(20) DEFAULT null,
	_fax	character varying(50) DEFAULT null,
	_tax_code	character varying(25) DEFAULT null,
	_remark	character varying DEFAULT null,
	_contact_person_1	character varying(255) DEFAULT null,
	_contact_phone_1	character varying(50) DEFAULT null,
	_contact_mobile_1	character varying(50) DEFAULT null,
	_contact_email_1	character varying(255) DEFAULT null,
	_contact_person_2	character varying(255) DEFAULT null,
	_contact_phone_2	character varying(50) DEFAULT null,
	_contact_mobile_2	character varying(50) DEFAULT null,
	_contact_email_2	character varying(255) DEFAULT null,
	_id_place	integer DEFAULT null,
	_map_object	oid DEFAULT null,
	_map_file	bytea DEFAULT null,
	_map_path	character varying(255) DEFAULT null,
	_description	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_is_provider	boolean DEFAULT null,
	_is_client	boolean DEFAULT null,
	_is_broker	boolean DEFAULT null
)
RETURNS SETOF company
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM company WHERE
	 	(_id is null OR id = _id)	
	AND (_code is null OR code = _code)	
	AND (_is_competitor is null OR is_competitor = _is_competitor)	
	AND (_abb_name is null OR abb_name = _abb_name)	
	AND (_short_name is null OR short_name = _short_name)	
	AND (_full_name is null OR full_name = _full_name)	
	AND (_foreign_name is null OR foreign_name = _foreign_name)	
	AND (_address is null OR address = _address)	
	AND (_telephone is null OR telephone = _telephone)	
	AND (_fax is null OR fax = _fax)	
	AND (_tax_code is null OR tax_code = _tax_code)	
	AND (_remark is null OR remark = _remark)	
	AND (_contact_person_1 is null OR contact_person_1 = _contact_person_1)	
	AND (_contact_phone_1 is null OR contact_phone_1 = _contact_phone_1)	
	AND (_contact_mobile_1 is null OR contact_mobile_1 = _contact_mobile_1)	
	AND (_contact_email_1 is null OR contact_email_1 = _contact_email_1)	
	AND (_contact_person_2 is null OR contact_person_2 = _contact_person_2)	
	AND (_contact_phone_2 is null OR contact_phone_2 = _contact_phone_2)	
	AND (_contact_mobile_2 is null OR contact_mobile_2 = _contact_mobile_2)	
	AND (_contact_email_2 is null OR contact_email_2 = _contact_email_2)	
	AND (_id_place is null OR id_place = _id_place)	
	AND (_map_object is null OR map_object = _map_object)	
	AND (_map_file is null OR map_file = _map_file)	
	AND (_map_path is null OR map_path = _map_path)	
	AND (_description is null OR description = _description)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_is_provider is null OR is_provider = _is_provider)	
	AND (_is_client is null OR is_client = _is_client)	
	AND (_is_broker is null OR is_broker = _is_broker)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_company_count (
	_id	integer DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_is_competitor	boolean DEFAULT null,
	_abb_name	character varying(10) DEFAULT null,
	_short_name	character varying(25) DEFAULT null,
	_full_name	character varying(255) DEFAULT null,
	_foreign_name	character varying(255) DEFAULT null,
	_address	character varying(255) DEFAULT null,
	_telephone	character varying(20) DEFAULT null,
	_fax	character varying(50) DEFAULT null,
	_tax_code	character varying(25) DEFAULT null,
	_remark	character varying DEFAULT null,
	_contact_person_1	character varying(255) DEFAULT null,
	_contact_phone_1	character varying(50) DEFAULT null,
	_contact_mobile_1	character varying(50) DEFAULT null,
	_contact_email_1	character varying(255) DEFAULT null,
	_contact_person_2	character varying(255) DEFAULT null,
	_contact_phone_2	character varying(50) DEFAULT null,
	_contact_mobile_2	character varying(50) DEFAULT null,
	_contact_email_2	character varying(255) DEFAULT null,
	_id_place	integer DEFAULT null,
	_map_object	oid DEFAULT null,
	_map_file	bytea DEFAULT null,
	_map_path	character varying(255) DEFAULT null,
	_description	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_is_provider	boolean DEFAULT null,
	_is_client	boolean DEFAULT null,
	_is_broker	boolean DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM company WHERE
	 	(_id is null OR id = _id)	
	AND (_code is null OR code = _code)	
	AND (_is_competitor is null OR is_competitor = _is_competitor)	
	AND (_abb_name is null OR abb_name = _abb_name)	
	AND (_short_name is null OR short_name = _short_name)	
	AND (_full_name is null OR full_name = _full_name)	
	AND (_foreign_name is null OR foreign_name = _foreign_name)	
	AND (_address is null OR address = _address)	
	AND (_telephone is null OR telephone = _telephone)	
	AND (_fax is null OR fax = _fax)	
	AND (_tax_code is null OR tax_code = _tax_code)	
	AND (_remark is null OR remark = _remark)	
	AND (_contact_person_1 is null OR contact_person_1 = _contact_person_1)	
	AND (_contact_phone_1 is null OR contact_phone_1 = _contact_phone_1)	
	AND (_contact_mobile_1 is null OR contact_mobile_1 = _contact_mobile_1)	
	AND (_contact_email_1 is null OR contact_email_1 = _contact_email_1)	
	AND (_contact_person_2 is null OR contact_person_2 = _contact_person_2)	
	AND (_contact_phone_2 is null OR contact_phone_2 = _contact_phone_2)	
	AND (_contact_mobile_2 is null OR contact_mobile_2 = _contact_mobile_2)	
	AND (_contact_email_2 is null OR contact_email_2 = _contact_email_2)	
	AND (_id_place is null OR id_place = _id_place)	
	AND (_map_object is null OR map_object = _map_object)	
	AND (_map_file is null OR map_file = _map_file)	
	AND (_map_path is null OR map_path = _map_path)	
	AND (_description is null OR description = _description)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_is_provider is null OR is_provider = _is_provider)	
	AND (_is_client is null OR is_client = _is_client)	
	AND (_is_broker is null OR is_broker = _is_broker);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_company_update 
(	_id integer,
	_code character varying(20),
	_is_competitor boolean,
	_abb_name character varying(10),
	_short_name character varying(25),
	_full_name character varying(255),
	_foreign_name character varying(255),
	_address character varying(255),
	_telephone character varying(20),
	_fax character varying(50),
	_tax_code character varying(25),
	_remark character varying,
	_contact_person_1 character varying(255),
	_contact_phone_1 character varying(50),
	_contact_mobile_1 character varying(50),
	_contact_email_1 character varying(255),
	_contact_person_2 character varying(255),
	_contact_phone_2 character varying(50),
	_contact_mobile_2 character varying(50),
	_contact_email_2 character varying(255),
	_id_place integer,
	_map_object oid,
	_map_file bytea,
	_map_path character varying(255),
	_description character varying,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone,
	_is_provider boolean,
	_is_client boolean,
	_is_broker boolean  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE company 
	SET 		code = _code,
		is_competitor = _is_competitor,
		abb_name = _abb_name,
		short_name = _short_name,
		full_name = _full_name,
		foreign_name = _foreign_name,
		address = _address,
		telephone = _telephone,
		fax = _fax,
		tax_code = _tax_code,
		remark = _remark,
		contact_person_1 = _contact_person_1,
		contact_phone_1 = _contact_phone_1,
		contact_mobile_1 = _contact_mobile_1,
		contact_email_1 = _contact_email_1,
		contact_person_2 = _contact_person_2,
		contact_phone_2 = _contact_phone_2,
		contact_mobile_2 = _contact_mobile_2,
		contact_email_2 = _contact_email_2,
		id_place = _id_place,
		map_object = _map_object,
		map_file = _map_file,
		map_path = _map_path,
		description = _description,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time,
		is_provider = _is_provider,
		is_client = _is_client,
		is_broker = _is_broker
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of company

-- company_competitor
CREATE OR REPLACE FUNCTION sp_company_competitor_insert(
	_id_company	integer DEFAULT null,
	_id_industry	integer DEFAULT null,
	_id_brand	integer DEFAULT null,
	_id_competitor	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_from_date	timestamp without time zone DEFAULT null,
	_to_date	timestamp without time zone DEFAULT null,
	_level	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO company_competitor(id_company,id_industry,id_brand,id_competitor,note,from_date,to_date,level,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_company,_id_industry,_id_brand,_id_competitor,_note,_from_date,_to_date,_level,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('company_competitor_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_company_competitor_select(_id	integer)
RETURNS SETOF company_competitor
AS $$
BEGIN
	RETURN QUERY SELECT * FROM company_competitor WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_company_competitor_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_id_industry	integer DEFAULT null,
	_id_brand	integer DEFAULT null,
	_id_competitor	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_from_dateFrom	timestamp without time zone DEFAULT null,
	_from_dateTo	timestamp without time zone DEFAULT null,
	_to_dateFrom	timestamp without time zone DEFAULT null,
	_to_dateTo	timestamp without time zone DEFAULT null,
	_level	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF company_competitor
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM company_competitor WHERE
	 	(_id is null OR id = _id)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_id_industry is null OR id_industry = _id_industry)	
	AND (_id_brand is null OR id_brand = _id_brand)	
	AND (_id_competitor is null OR id_competitor = _id_competitor)	
	AND (_note is null OR note = _note)	
	AND (_from_dateFrom is null OR from_date >= _from_dateFrom)
	AND	(_from_dateTo is null OR from_date <= _from_dateTo)	
	AND (_to_dateFrom is null OR to_date >= _to_dateFrom)
	AND	(_to_dateTo is null OR to_date <= _to_dateTo)	
	AND (_level is null OR level = _level)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_company_competitor_count (
	_id	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_id_industry	integer DEFAULT null,
	_id_brand	integer DEFAULT null,
	_id_competitor	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_from_dateFrom	timestamp without time zone DEFAULT null,
	_from_dateTo	timestamp without time zone DEFAULT null,
	_to_dateFrom	timestamp without time zone DEFAULT null,
	_to_dateTo	timestamp without time zone DEFAULT null,
	_level	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM company_competitor WHERE
	 	(_id is null OR id = _id)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_id_industry is null OR id_industry = _id_industry)	
	AND (_id_brand is null OR id_brand = _id_brand)	
	AND (_id_competitor is null OR id_competitor = _id_competitor)	
	AND (_note is null OR note = _note)	
	AND (_from_dateFrom is null OR from_date >= _from_dateFrom)
	AND	(_from_dateTo is null OR from_date <= _from_dateTo)	
	AND (_to_dateFrom is null OR to_date >= _to_dateFrom)
	AND	(_to_dateTo is null OR to_date <= _to_dateTo)	
	AND (_level is null OR level = _level)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_company_competitor_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM company_competitor
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_company_competitor_update 
(	_id integer,
	_id_company integer,
	_id_industry integer,
	_id_brand integer,
	_id_competitor integer,
	_note character varying,
	_from_date timestamp without time zone,
	_to_date timestamp without time zone,
	_level integer,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE company_competitor 
	SET 		id_company = _id_company,
		id_industry = _id_industry,
		id_brand = _id_brand,
		id_competitor = _id_competitor,
		note = _note,
		from_date = _from_date,
		to_date = _to_date,
		level = _level,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of company_competitor

-- company_industry
CREATE OR REPLACE FUNCTION sp_company_industry_insert(
	_id_company	integer DEFAULT null,
	_id_industry	integer DEFAULT null,
	_id_brand	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO company_industry(id_company,id_industry,id_brand,note,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_company,_id_industry,_id_brand,_note,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('company_industry_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_company_industry_select(_id	integer)
RETURNS SETOF company_industry
AS $$
BEGIN
	RETURN QUERY SELECT * FROM company_industry WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_company_industry_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_id_industry	integer DEFAULT null,
	_id_brand	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF company_industry
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM company_industry WHERE
	 	(_id is null OR id = _id)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_id_industry is null OR id_industry = _id_industry)	
	AND (_id_brand is null OR id_brand = _id_brand)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_company_industry_count (
	_id	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_id_industry	integer DEFAULT null,
	_id_brand	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM company_industry WHERE
	 	(_id is null OR id = _id)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_id_industry is null OR id_industry = _id_industry)	
	AND (_id_brand is null OR id_brand = _id_brand)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_company_industry_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM company_industry
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_company_industry_update 
(	_id integer,
	_id_company integer,
	_id_industry integer,
	_id_brand integer,
	_note character varying,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE company_industry 
	SET 		id_company = _id_company,
		id_industry = _id_industry,
		id_brand = _id_brand,
		note = _note,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of company_industry

-- company_product_category
CREATE OR REPLACE FUNCTION sp_company_product_category_insert(
	_id_company	integer,
	_id_category	integer,
	_note	character varying DEFAULT null,
	_from_date	timestamp without time zone DEFAULT null,
	_to_date	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO company_product_category(id_company,id_category,note,from_date,to_date,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_company,_id_category,_note,_from_date,_to_date,_status,_is_draft,_notification,_creator,_creator,null);
RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_company_product_category_select(_id_company	integer , _id_category	integer)
RETURNS SETOF company_product_category
AS $$
BEGIN
	RETURN QUERY SELECT * FROM company_product_category WHERE id_company = _id_company AND id_category = _id_category;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_company_product_category_selectList (
	_limit	integer,
	_offset integer,
	_id_company	integer DEFAULT null,
	_id_category	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_from_dateFrom	timestamp without time zone DEFAULT null,
	_from_dateTo	timestamp without time zone DEFAULT null,
	_to_dateFrom	timestamp without time zone DEFAULT null,
	_to_dateTo	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF company_product_category
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM company_product_category WHERE
	 	(_id_company is null OR id_company = _id_company)	
	AND (_id_category is null OR id_category = _id_category)	
	AND (_note is null OR note = _note)	
	AND (_from_dateFrom is null OR from_date >= _from_dateFrom)
	AND	(_from_dateTo is null OR from_date <= _from_dateTo)	
	AND (_to_dateFrom is null OR to_date >= _to_dateFrom)
	AND	(_to_dateTo is null OR to_date <= _to_dateTo)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_company_product_category_count (
	_id_company	integer DEFAULT null,
	_id_category	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_from_dateFrom	timestamp without time zone DEFAULT null,
	_from_dateTo	timestamp without time zone DEFAULT null,
	_to_dateFrom	timestamp without time zone DEFAULT null,
	_to_dateTo	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM company_product_category WHERE
	 	(_id_company is null OR id_company = _id_company)	
	AND (_id_category is null OR id_category = _id_category)	
	AND (_note is null OR note = _note)	
	AND (_from_dateFrom is null OR from_date >= _from_dateFrom)
	AND	(_from_dateTo is null OR from_date <= _from_dateTo)	
	AND (_to_dateFrom is null OR to_date >= _to_dateFrom)
	AND	(_to_dateTo is null OR to_date <= _to_dateTo)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_company_product_category_delete(_id_company integer,	_id_category integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM company_product_category
 		WHERE "id_company" = _id_company AND "id_category" = _id_category;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_company_product_category_update 
(	_id_company integer,
	_id_category integer,
	_note character varying,
	_from_date timestamp without time zone,
	_to_date timestamp without time zone,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE company_product_category 
	SET 		note = _note,
		from_date = _from_date,
		to_date = _to_date,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id_company = _id_company and id_category = _id_category;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of company_product_category

-- contact
CREATE OR REPLACE FUNCTION sp_contact_insert(
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_description	character varying DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_mobile	character varying(20) DEFAULT null,
	_phone	character varying(20) DEFAULT null,
	_id_person	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO contact(object_type,object_id,description,name,mobile,phone,id_person,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_object_type,_object_id,_description,_name,_mobile,_phone,_id_person,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('contact_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_contact_select(_id	integer)
RETURNS SETOF contact
AS $$
BEGIN
	RETURN QUERY SELECT * FROM contact WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_contact_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_description	character varying DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_mobile	character varying(20) DEFAULT null,
	_phone	character varying(20) DEFAULT null,
	_id_person	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF contact
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM contact WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_description is null OR description = _description)	
	AND (_name is null OR name = _name)	
	AND (_mobile is null OR mobile = _mobile)	
	AND (_phone is null OR phone = _phone)	
	AND (_id_person is null OR id_person = _id_person)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_contact_count (
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_description	character varying DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_mobile	character varying(20) DEFAULT null,
	_phone	character varying(20) DEFAULT null,
	_id_person	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM contact WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_description is null OR description = _description)	
	AND (_name is null OR name = _name)	
	AND (_mobile is null OR mobile = _mobile)	
	AND (_phone is null OR phone = _phone)	
	AND (_id_person is null OR id_person = _id_person)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_contact_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM contact
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_contact_update 
(	_id integer,
	_object_type character varying(50),
	_object_id integer,
	_description character varying,
	_name character varying(255),
	_mobile character varying(20),
	_phone character varying(20),
	_id_person integer,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE contact 
	SET 		object_type = _object_type,
		object_id = _object_id,
		description = _description,
		name = _name,
		mobile = _mobile,
		phone = _phone,
		id_person = _id_person,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of contact

-- currency
CREATE OR REPLACE FUNCTION sp_currency_select(_id	integer)
RETURNS SETOF currency
AS $$
BEGIN
	RETURN QUERY SELECT * FROM currency WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_currency_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM currency
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_currency_insert(
	_name	character varying(255),
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_abbreviation	character varying(10) DEFAULT null,
	_foreign_name	character varying(255) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null,
	_is_default	boolean DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO currency(name,code,description,abbreviation,foreign_name,status,is_draft,notification,creator,updator,deletion_time,is_default)
	VALUES(_name,_code,_description,_abbreviation,_foreign_name,_status,_is_draft,_notification,_creator,_creator,null,_is_default);
RETURN currval('currency_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_currency_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_abbreviation	character varying(10) DEFAULT null,
	_foreign_name	character varying(255) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_is_default	boolean DEFAULT null
)
RETURNS SETOF currency
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM currency WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_abbreviation is null OR abbreviation = _abbreviation)	
	AND (_foreign_name is null OR foreign_name = _foreign_name)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_is_default is null OR is_default = _is_default)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_currency_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_abbreviation	character varying(10) DEFAULT null,
	_foreign_name	character varying(255) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_is_default	boolean DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM currency WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_abbreviation is null OR abbreviation = _abbreviation)	
	AND (_foreign_name is null OR foreign_name = _foreign_name)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_is_default is null OR is_default = _is_default);
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION sp_currency_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_abbreviation character varying(10),
	_foreign_name character varying(255),
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone,
	_is_default boolean  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE currency 
	SET 		name = _name,
		code = _code,
		description = _description,
		abbreviation = _abbreviation,
		foreign_name = _foreign_name,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time,
		is_default = _is_default
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of currency

-- dependence
CREATE OR REPLACE FUNCTION sp_dependence_insert(
	_id_task	integer,
	_id_prev_task	integer,
	_note	character varying DEFAULT null,
	_level	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO dependence(id_task,id_prev_task,note,level,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_task,_id_prev_task,_note,_level,_is_draft,_notification,_creator,_creator,null);
RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_dependence_select(_id_task	integer , _id_prev_task	integer)
RETURNS SETOF dependence
AS $$
BEGIN
	RETURN QUERY SELECT * FROM dependence WHERE id_task = _id_task AND id_prev_task = _id_prev_task;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_dependence_selectList (
	_limit	integer,
	_offset integer,
	_id_task	integer DEFAULT null,
	_id_prev_task	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_level	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF dependence
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM dependence WHERE
	 	(_id_task is null OR id_task = _id_task)	
	AND (_id_prev_task is null OR id_prev_task = _id_prev_task)	
	AND (_note is null OR note = _note)	
	AND (_level is null OR level = _level)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_dependence_count (
	_id_task	integer DEFAULT null,
	_id_prev_task	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_level	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM dependence WHERE
	 	(_id_task is null OR id_task = _id_task)	
	AND (_id_prev_task is null OR id_prev_task = _id_prev_task)	
	AND (_note is null OR note = _note)	
	AND (_level is null OR level = _level)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_dependence_delete(_id_task integer,	_id_prev_task integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM dependence
 		WHERE "id_task" = _id_task AND "id_prev_task" = _id_prev_task;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_dependence_update 
(	_id_task integer,
	_id_prev_task integer,
	_note character varying,
	_level integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE dependence 
	SET 		note = _note,
		level = _level,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id_task = _id_task and id_prev_task = _id_prev_task;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of dependence

-- document
CREATE OR REPLACE FUNCTION sp_document_select(_id	integer)
RETURNS SETOF document
AS $$
BEGIN
	RETURN QUERY SELECT * FROM document WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_document_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM document
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_document_insert(
	_code	character varying(20) DEFAULT null,
	_is_template	boolean DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null,
	_document_name	character varying(255) DEFAULT null,
	_extension	character varying(20) DEFAULT null,
	_description	character varying(1000) DEFAULT null,
	_remark	character varying(1000) DEFAULT null,
	_document_type	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO document(code,is_template,object_type,object_id,status,notification,creator,updator,deletion_time,document_name,extension,description,remark,document_type)
	VALUES(_code,_is_template,_object_type,_object_id,_status,_notification,_creator,_creator,null,_document_name,_extension,_description,_remark,_document_type);
RETURN currval('document_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_document_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_is_template	boolean DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_document_name	character varying(255) DEFAULT null,
	_extension	character varying(20) DEFAULT null,
	_description	character varying(1000) DEFAULT null,
	_remark	character varying(1000) DEFAULT null,
	_document_type	integer DEFAULT null
)
RETURNS SETOF document
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM document WHERE
	 	(_id is null OR id = _id)	
	AND (_code is null OR code = _code)	
	AND (_is_template is null OR is_template = _is_template)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_status is null OR status = _status)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_document_name is null OR document_name = _document_name)	
	AND (_extension is null OR extension = _extension)	
	AND (_description is null OR description = _description)	
	AND (_remark is null OR remark = _remark)	
	AND (_document_type is null OR document_type = _document_type)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_document_count (
	_id	integer DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_is_template	boolean DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_document_name	character varying(255) DEFAULT null,
	_extension	character varying(20) DEFAULT null,
	_description	character varying(1000) DEFAULT null,
	_remark	character varying(1000) DEFAULT null,
	_document_type	integer DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM document WHERE
	 	(_id is null OR id = _id)	
	AND (_code is null OR code = _code)	
	AND (_is_template is null OR is_template = _is_template)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_status is null OR status = _status)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_document_name is null OR document_name = _document_name)	
	AND (_extension is null OR extension = _extension)	
	AND (_description is null OR description = _description)	
	AND (_remark is null OR remark = _remark)	
	AND (_document_type is null OR document_type = _document_type);
END;
$$ LANGUAGE plpgsql;



CREATE OR REPLACE FUNCTION sp_document_update 
(	_id integer,
	_code character varying(20),
	_is_template boolean,
	_object_type character varying(50),
	_object_id integer,
	_status integer,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone,
	_document_name character varying(255),
	_extension character varying(20),
	_description character varying(1000),
	_remark character varying(1000),
	_document_type integer  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE document 
	SET 		code = _code,
		is_template = _is_template,
		object_type = _object_type,
		object_id = _object_id,
		status = _status,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time,
		document_name = _document_name,
		extension = _extension,
		description = _description,
		remark = _remark,
		document_type = _document_type
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;
-- end of document

-- document_type
CREATE OR REPLACE FUNCTION sp_document_type_select(_id	integer)
RETURNS SETOF document_type
AS $$
BEGIN
	RETURN QUERY SELECT * FROM document_type WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_document_type_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM document_type
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_document_type_insert(
	_name	character varying(255),
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO document_type(name,code,description,id_parent,status,creator,updator,deletion_time)
	VALUES(_name,_code,_description,_id_parent,_status,_creator,_creator,null);
RETURN currval('document_type_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';



CREATE OR REPLACE FUNCTION sp_document_type_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF document_type
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM document_type WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_status is null OR status = _status)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_document_type_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM document_type WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_status is null OR status = _status)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;



CREATE OR REPLACE FUNCTION sp_document_type_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_id_parent integer,
	_status integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE document_type 
	SET 		name = _name,
		code = _code,
		description = _description,
		id_parent = _id_parent,
		status = _status,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of document_type

-- document_version
CREATE OR REPLACE FUNCTION sp_document_version_insert(
	_id_document	integer,
	_version	integer,
	_is_last_version	boolean DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_file	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO document_version(id_document,version,is_last_version,name,code,description,id_file,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_document,_version,_is_last_version,_name,_code,_description,_id_file,_status,_is_draft,_notification,_creator,_creator,null);
RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_document_version_select(_id_document	integer , _version	integer)
RETURNS SETOF document_version
AS $$
BEGIN
	RETURN QUERY SELECT * FROM document_version WHERE id_document = _id_document AND version = _version;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_document_version_selectList (
	_limit	integer,
	_offset integer,
	_id_document	integer DEFAULT null,
	_version	integer DEFAULT null,
	_is_last_version	boolean DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_file	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF document_version
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM document_version WHERE
	 	(_id_document is null OR id_document = _id_document)	
	AND (_version is null OR version = _version)	
	AND (_is_last_version is null OR is_last_version = _is_last_version)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_file is null OR id_file = _id_file)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_document_version_count (
	_id_document	integer DEFAULT null,
	_version	integer DEFAULT null,
	_is_last_version	boolean DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_file	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM document_version WHERE
	 	(_id_document is null OR id_document = _id_document)	
	AND (_version is null OR version = _version)	
	AND (_is_last_version is null OR is_last_version = _is_last_version)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_file is null OR id_file = _id_file)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_document_version_delete(_id_document integer,	_version integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM document_version
 		WHERE "id_document" = _id_document AND "version" = _version;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_document_version_update 
(	_id_document integer,
	_version integer,
	_is_last_version boolean,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_id_file integer,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE document_version 
	SET 		is_last_version = _is_last_version,
		name = _name,
		code = _code,
		description = _description,
		id_file = _id_file,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id_document = _id_document and version = _version;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of document_version

-- employee
CREATE OR REPLACE FUNCTION sp_employee_insert(
	_code	character varying(20) DEFAULT null,
	_note	character varying DEFAULT null,
	_user_name	character varying(255) DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_id_person	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO employee(code,note,user_name,name,id_person,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_code,_note,_user_name,_name,_id_person,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('employee_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_employee_select(_id	integer)
RETURNS SETOF employee
AS $$
BEGIN
	RETURN QUERY SELECT * FROM employee WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_employee_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_note	character varying DEFAULT null,
	_user_name	character varying(255) DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_id_person	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF employee
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM employee WHERE
	 	(_id is null OR id = _id)	
	AND (_code is null OR code = _code)	
	AND (_note is null OR note = _note)	
	AND (_user_name is null OR user_name = _user_name)	
	AND (_name is null OR name = _name)	
	AND (_id_person is null OR id_person = _id_person)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_employee_count (
	_id	integer DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_note	character varying DEFAULT null,
	_user_name	character varying(255) DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_id_person	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM employee WHERE
	 	(_id is null OR id = _id)	
	AND (_code is null OR code = _code)	
	AND (_note is null OR note = _note)	
	AND (_user_name is null OR user_name = _user_name)	
	AND (_name is null OR name = _name)	
	AND (_id_person is null OR id_person = _id_person)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_employee_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM employee
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_employee_update 
(	_id integer,
	_code character varying(20),
	_note character varying,
	_user_name character varying(255),
	_name character varying(255),
	_id_person integer,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE employee 
	SET 		code = _code,
		note = _note,
		user_name = _user_name,
		name = _name,
		id_person = _id_person,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of employee

-- employee_position
CREATE OR REPLACE FUNCTION sp_employee_position_insert(
	_id_employee	integer DEFAULT null,
	_id_position	integer DEFAULT null,
	_from_date	timestamp without time zone DEFAULT null,
	_to_date	timestamp without time zone DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO employee_position(id_employee,id_position,from_date,to_date,note,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_employee,_id_position,_from_date,_to_date,_note,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('employee_position_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_employee_position_select(_id	integer)
RETURNS SETOF employee_position
AS $$
BEGIN
	RETURN QUERY SELECT * FROM employee_position WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_employee_position_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_position	integer DEFAULT null,
	_from_dateFrom	timestamp without time zone DEFAULT null,
	_from_dateTo	timestamp without time zone DEFAULT null,
	_to_dateFrom	timestamp without time zone DEFAULT null,
	_to_dateTo	timestamp without time zone DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF employee_position
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM employee_position WHERE
	 	(_id is null OR id = _id)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_position is null OR id_position = _id_position)	
	AND (_from_dateFrom is null OR from_date >= _from_dateFrom)
	AND	(_from_dateTo is null OR from_date <= _from_dateTo)	
	AND (_to_dateFrom is null OR to_date >= _to_dateFrom)
	AND	(_to_dateTo is null OR to_date <= _to_dateTo)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_employee_position_count (
	_id	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_position	integer DEFAULT null,
	_from_dateFrom	timestamp without time zone DEFAULT null,
	_from_dateTo	timestamp without time zone DEFAULT null,
	_to_dateFrom	timestamp without time zone DEFAULT null,
	_to_dateTo	timestamp without time zone DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM employee_position WHERE
	 	(_id is null OR id = _id)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_position is null OR id_position = _id_position)	
	AND (_from_dateFrom is null OR from_date >= _from_dateFrom)
	AND	(_from_dateTo is null OR from_date <= _from_dateTo)	
	AND (_to_dateFrom is null OR to_date >= _to_dateFrom)
	AND	(_to_dateTo is null OR to_date <= _to_dateTo)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_employee_position_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM employee_position
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_employee_position_update 
(	_id integer,
	_id_employee integer,
	_id_position integer,
	_from_date timestamp without time zone,
	_to_date timestamp without time zone,
	_note character varying,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE employee_position 
	SET 		id_employee = _id_employee,
		id_position = _id_position,
		from_date = _from_date,
		to_date = _to_date,
		note = _note,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of employee_position

-- employee_right
CREATE OR REPLACE FUNCTION sp_employee_right_insert(
	_id_employee	integer,
	_id_role	integer,
	_right_code	integer,
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_scope	integer DEFAULT null,
	_quota	numeric DEFAULT null,
	_note	character varying(511) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO employee_right(id_employee,id_role,right_code,object_type,object_id,scope,quota,note,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_employee,_id_role,_right_code,_object_type,_object_id,_scope,_quota,_note,_status,_is_draft,_notification,_creator,_creator,null);
RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_employee_right_select(_id_employee	integer , _id_role	integer , _right_code	integer)
RETURNS SETOF employee_right
AS $$
BEGIN
	RETURN QUERY SELECT * FROM employee_right WHERE id_employee = _id_employee AND id_role = _id_role AND right_code = _right_code;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_employee_right_selectList (
	_limit	integer,
	_offset integer,
	_id_employee	integer DEFAULT null,
	_id_role	integer DEFAULT null,
	_right_code	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_scope	integer DEFAULT null,
	_quota	numeric DEFAULT null,
	_note	character varying(511) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF employee_right
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM employee_right WHERE
	 	(_id_employee is null OR id_employee = _id_employee)	
	AND (_id_role is null OR id_role = _id_role)	
	AND (_right_code is null OR right_code = _right_code)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_scope is null OR scope = _scope)	
	AND (_quota is null OR quota = _quota)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_employee_right_count (
	_id_employee	integer DEFAULT null,
	_id_role	integer DEFAULT null,
	_right_code	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_scope	integer DEFAULT null,
	_quota	numeric DEFAULT null,
	_note	character varying(511) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM employee_right WHERE
	 	(_id_employee is null OR id_employee = _id_employee)	
	AND (_id_role is null OR id_role = _id_role)	
	AND (_right_code is null OR right_code = _right_code)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_scope is null OR scope = _scope)	
	AND (_quota is null OR quota = _quota)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_employee_right_delete(_id_employee integer,	_id_role integer,	_right_code integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM employee_right
 		WHERE "id_employee" = _id_employee AND "id_role" = _id_role AND "right_code" = _right_code;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_employee_right_update 
(	_id_employee integer,
	_id_role integer,
	_right_code integer,
	_object_type character varying(50),
	_object_id integer,
	_scope integer,
	_quota numeric,
	_note character varying(511),
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE employee_right 
	SET 		object_type = _object_type,
		object_id = _object_id,
		scope = _scope,
		quota = _quota,
		note = _note,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id_employee = _id_employee and id_role = _id_role and right_code = _right_code;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of employee_right

-- employee_role
CREATE OR REPLACE FUNCTION sp_employee_role_insert(
	_id_employee	integer,
	_id_role	integer,
	_from_date	timestamp without time zone DEFAULT null,
	_level	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO employee_role(id_employee,id_role,from_date,level,note,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_employee,_id_role,_from_date,_level,_note,_status,_is_draft,_notification,_creator,_creator,null);
RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_employee_role_select(_id_employee	integer , _id_role	integer)
RETURNS SETOF employee_role
AS $$
BEGIN
	RETURN QUERY SELECT * FROM employee_role WHERE id_employee = _id_employee AND id_role = _id_role;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_employee_role_selectList (
	_limit	integer,
	_offset integer,
	_id_employee	integer DEFAULT null,
	_id_role	integer DEFAULT null,
	_from_dateFrom	timestamp without time zone DEFAULT null,
	_from_dateTo	timestamp without time zone DEFAULT null,
	_level	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF employee_role
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM employee_role WHERE
	 	(_id_employee is null OR id_employee = _id_employee)	
	AND (_id_role is null OR id_role = _id_role)	
	AND (_from_dateFrom is null OR from_date >= _from_dateFrom)
	AND	(_from_dateTo is null OR from_date <= _from_dateTo)	
	AND (_level is null OR level = _level)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_employee_role_count (
	_id_employee	integer DEFAULT null,
	_id_role	integer DEFAULT null,
	_from_dateFrom	timestamp without time zone DEFAULT null,
	_from_dateTo	timestamp without time zone DEFAULT null,
	_level	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM employee_role WHERE
	 	(_id_employee is null OR id_employee = _id_employee)	
	AND (_id_role is null OR id_role = _id_role)	
	AND (_from_dateFrom is null OR from_date >= _from_dateFrom)
	AND	(_from_dateTo is null OR from_date <= _from_dateTo)	
	AND (_level is null OR level = _level)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_employee_role_delete(_id_employee integer,	_id_role integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM employee_role
 		WHERE "id_employee" = _id_employee AND "id_role" = _id_role;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_employee_role_update 
(	_id_employee integer,
	_id_role integer,
	_from_date timestamp without time zone,
	_level integer,
	_note character varying,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE employee_role 
	SET 		from_date = _from_date,
		level = _level,
		note = _note,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id_employee = _id_employee and id_role = _id_role;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of employee_role

-- event
CREATE OR REPLACE FUNCTION sp_event_insert(
	_id_project	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_task	integer DEFAULT null,
	_id_type	integer DEFAULT null,
	_id_place	integer DEFAULT null,
	_id_venue	integer DEFAULT null,
	_plan_start_time	timestamp without time zone DEFAULT null,
	_plan_end_time	timestamp without time zone DEFAULT null,
	_real_start_tate	timestamp without time zone DEFAULT null,
	_real_end_time	timestamp without time zone DEFAULT null,
	_need_approval	boolean DEFAULT null,
	_is_event	boolean DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO event(id_project,name,code,description,id_task,id_type,id_place,id_venue,plan_start_time,plan_end_time,real_start_tate,real_end_time,need_approval,is_event,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_project,_name,_code,_description,_id_task,_id_type,_id_place,_id_venue,_plan_start_time,_plan_end_time,_real_start_tate,_real_end_time,_need_approval,_is_event,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('event_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_event_select(_id	integer)
RETURNS SETOF event
AS $$
BEGIN
	RETURN QUERY SELECT * FROM event WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_event_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_task	integer DEFAULT null,
	_id_type	integer DEFAULT null,
	_id_place	integer DEFAULT null,
	_id_venue	integer DEFAULT null,
	_plan_start_timeFrom	timestamp without time zone DEFAULT null,
	_plan_start_timeTo	timestamp without time zone DEFAULT null,
	_plan_end_timeFrom	timestamp without time zone DEFAULT null,
	_plan_end_timeTo	timestamp without time zone DEFAULT null,
	_real_start_tateFrom	timestamp without time zone DEFAULT null,
	_real_start_tateTo	timestamp without time zone DEFAULT null,
	_real_end_timeFrom	timestamp without time zone DEFAULT null,
	_real_end_timeTo	timestamp without time zone DEFAULT null,
	_need_approval	boolean DEFAULT null,
	_is_event	boolean DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF event
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM event WHERE
	 	(_id is null OR id = _id)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_task is null OR id_task = _id_task)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_id_place is null OR id_place = _id_place)	
	AND (_id_venue is null OR id_venue = _id_venue)	
	AND (_plan_start_timeFrom is null OR plan_start_time >= _plan_start_timeFrom)
	AND	(_plan_start_timeTo is null OR plan_start_time <= _plan_start_timeTo)	
	AND (_plan_end_timeFrom is null OR plan_end_time >= _plan_end_timeFrom)
	AND	(_plan_end_timeTo is null OR plan_end_time <= _plan_end_timeTo)	
	AND (_real_start_tateFrom is null OR real_start_tate >= _real_start_tateFrom)
	AND	(_real_start_tateTo is null OR real_start_tate <= _real_start_tateTo)	
	AND (_real_end_timeFrom is null OR real_end_time >= _real_end_timeFrom)
	AND	(_real_end_timeTo is null OR real_end_time <= _real_end_timeTo)	
	AND (_need_approval is null OR need_approval = _need_approval)	
	AND (_is_event is null OR is_event = _is_event)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_event_count (
	_id	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_task	integer DEFAULT null,
	_id_type	integer DEFAULT null,
	_id_place	integer DEFAULT null,
	_id_venue	integer DEFAULT null,
	_plan_start_timeFrom	timestamp without time zone DEFAULT null,
	_plan_start_timeTo	timestamp without time zone DEFAULT null,
	_plan_end_timeFrom	timestamp without time zone DEFAULT null,
	_plan_end_timeTo	timestamp without time zone DEFAULT null,
	_real_start_tateFrom	timestamp without time zone DEFAULT null,
	_real_start_tateTo	timestamp without time zone DEFAULT null,
	_real_end_timeFrom	timestamp without time zone DEFAULT null,
	_real_end_timeTo	timestamp without time zone DEFAULT null,
	_need_approval	boolean DEFAULT null,
	_is_event	boolean DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM event WHERE
	 	(_id is null OR id = _id)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_task is null OR id_task = _id_task)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_id_place is null OR id_place = _id_place)	
	AND (_id_venue is null OR id_venue = _id_venue)	
	AND (_plan_start_timeFrom is null OR plan_start_time >= _plan_start_timeFrom)
	AND	(_plan_start_timeTo is null OR plan_start_time <= _plan_start_timeTo)	
	AND (_plan_end_timeFrom is null OR plan_end_time >= _plan_end_timeFrom)
	AND	(_plan_end_timeTo is null OR plan_end_time <= _plan_end_timeTo)	
	AND (_real_start_tateFrom is null OR real_start_tate >= _real_start_tateFrom)
	AND	(_real_start_tateTo is null OR real_start_tate <= _real_start_tateTo)	
	AND (_real_end_timeFrom is null OR real_end_time >= _real_end_timeFrom)
	AND	(_real_end_timeTo is null OR real_end_time <= _real_end_timeTo)	
	AND (_need_approval is null OR need_approval = _need_approval)	
	AND (_is_event is null OR is_event = _is_event)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_event_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM event
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_event_update 
(	_id integer,
	_id_project integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_id_task integer,
	_id_type integer,
	_id_place integer,
	_id_venue integer,
	_plan_start_time timestamp without time zone,
	_plan_end_time timestamp without time zone,
	_real_start_tate timestamp without time zone,
	_real_end_time timestamp without time zone,
	_need_approval boolean,
	_is_event boolean,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE event 
	SET 		id_project = _id_project,
		name = _name,
		code = _code,
		description = _description,
		id_task = _id_task,
		id_type = _id_type,
		id_place = _id_place,
		id_venue = _id_venue,
		plan_start_time = _plan_start_time,
		plan_end_time = _plan_end_time,
		real_start_tate = _real_start_tate,
		real_end_time = _real_end_time,
		need_approval = _need_approval,
		is_event = _is_event,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of event

-- event_attendee
CREATE OR REPLACE FUNCTION sp_event_attendee_insert(
	_id_event	integer DEFAULT null,
	_is_employee	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_person	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_id_role	integer DEFAULT null,
	_plan_start_time	timestamp without time zone DEFAULT null,
	_plan_end_time	timestamp without time zone DEFAULT null,
	_real_start_tate	timestamp without time zone DEFAULT null,
	_real_end_time	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO event_attendee(id_event,is_employee,id_employee,id_person,note,id_role,plan_start_time,plan_end_time,real_start_tate,real_end_time,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_event,_is_employee,_id_employee,_id_person,_note,_id_role,_plan_start_time,_plan_end_time,_real_start_tate,_real_end_time,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('event_attendee_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_event_attendee_select(_id	integer)
RETURNS SETOF event_attendee
AS $$
BEGIN
	RETURN QUERY SELECT * FROM event_attendee WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_event_attendee_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_event	integer DEFAULT null,
	_is_employee	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_person	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_id_role	integer DEFAULT null,
	_plan_start_timeFrom	timestamp without time zone DEFAULT null,
	_plan_start_timeTo	timestamp without time zone DEFAULT null,
	_plan_end_timeFrom	timestamp without time zone DEFAULT null,
	_plan_end_timeTo	timestamp without time zone DEFAULT null,
	_real_start_tateFrom	timestamp without time zone DEFAULT null,
	_real_start_tateTo	timestamp without time zone DEFAULT null,
	_real_end_timeFrom	timestamp without time zone DEFAULT null,
	_real_end_timeTo	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF event_attendee
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM event_attendee WHERE
	 	(_id is null OR id = _id)	
	AND (_id_event is null OR id_event = _id_event)	
	AND (_is_employee is null OR is_employee = _is_employee)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_person is null OR id_person = _id_person)	
	AND (_note is null OR note = _note)	
	AND (_id_role is null OR id_role = _id_role)	
	AND (_plan_start_timeFrom is null OR plan_start_time >= _plan_start_timeFrom)
	AND	(_plan_start_timeTo is null OR plan_start_time <= _plan_start_timeTo)	
	AND (_plan_end_timeFrom is null OR plan_end_time >= _plan_end_timeFrom)
	AND	(_plan_end_timeTo is null OR plan_end_time <= _plan_end_timeTo)	
	AND (_real_start_tateFrom is null OR real_start_tate >= _real_start_tateFrom)
	AND	(_real_start_tateTo is null OR real_start_tate <= _real_start_tateTo)	
	AND (_real_end_timeFrom is null OR real_end_time >= _real_end_timeFrom)
	AND	(_real_end_timeTo is null OR real_end_time <= _real_end_timeTo)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_event_attendee_count (
	_id	integer DEFAULT null,
	_id_event	integer DEFAULT null,
	_is_employee	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_person	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_id_role	integer DEFAULT null,
	_plan_start_timeFrom	timestamp without time zone DEFAULT null,
	_plan_start_timeTo	timestamp without time zone DEFAULT null,
	_plan_end_timeFrom	timestamp without time zone DEFAULT null,
	_plan_end_timeTo	timestamp without time zone DEFAULT null,
	_real_start_tateFrom	timestamp without time zone DEFAULT null,
	_real_start_tateTo	timestamp without time zone DEFAULT null,
	_real_end_timeFrom	timestamp without time zone DEFAULT null,
	_real_end_timeTo	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM event_attendee WHERE
	 	(_id is null OR id = _id)	
	AND (_id_event is null OR id_event = _id_event)	
	AND (_is_employee is null OR is_employee = _is_employee)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_person is null OR id_person = _id_person)	
	AND (_note is null OR note = _note)	
	AND (_id_role is null OR id_role = _id_role)	
	AND (_plan_start_timeFrom is null OR plan_start_time >= _plan_start_timeFrom)
	AND	(_plan_start_timeTo is null OR plan_start_time <= _plan_start_timeTo)	
	AND (_plan_end_timeFrom is null OR plan_end_time >= _plan_end_timeFrom)
	AND	(_plan_end_timeTo is null OR plan_end_time <= _plan_end_timeTo)	
	AND (_real_start_tateFrom is null OR real_start_tate >= _real_start_tateFrom)
	AND	(_real_start_tateTo is null OR real_start_tate <= _real_start_tateTo)	
	AND (_real_end_timeFrom is null OR real_end_time >= _real_end_timeFrom)
	AND	(_real_end_timeTo is null OR real_end_time <= _real_end_timeTo)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_event_attendee_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM event_attendee
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_event_attendee_update 
(	_id integer,
	_id_event integer,
	_is_employee integer,
	_id_employee integer,
	_id_person integer,
	_note character varying,
	_id_role integer,
	_plan_start_time timestamp without time zone,
	_plan_end_time timestamp without time zone,
	_real_start_tate timestamp without time zone,
	_real_end_time timestamp without time zone,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE event_attendee 
	SET 		id_event = _id_event,
		is_employee = _is_employee,
		id_employee = _id_employee,
		id_person = _id_person,
		note = _note,
		id_role = _id_role,
		plan_start_time = _plan_start_time,
		plan_end_time = _plan_end_time,
		real_start_tate = _real_start_tate,
		real_end_time = _real_end_time,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of event_attendee

-- event_type
CREATE OR REPLACE FUNCTION sp_event_type_insert(
	_name	character varying(255),
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO event_type(name,code,description,id_parent,created,creator,updated,updator,deletion_time)
	VALUES(_name,_code,_description,_id_parent,_created,_creator,_updated,_creator,null);
RETURN currval('event_type_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_event_type_select(_id	integer)
RETURNS SETOF event_type
AS $$
BEGIN
	RETURN QUERY SELECT * FROM event_type WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_event_type_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF event_type
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM event_type WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_event_type_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM event_type WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_event_type_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM event_type
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_event_type_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_id_parent integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE event_type 
	SET 		name = _name,
		code = _code,
		description = _description,
		id_parent = _id_parent,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of event_type

-- exchange_rate
CREATE OR REPLACE FUNCTION sp_exchange_rate_insert(
	_id_source_currency	integer DEFAULT null,
	_id_target_currency	integer DEFAULT null,
	_from_time	timestamp without time zone DEFAULT null,
	_to_time	timestamp without time zone DEFAULT null,
	_rate	numeric DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO exchange_rate(id_source_currency,id_target_currency,from_time,to_time,rate,note,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_source_currency,_id_target_currency,_from_time,_to_time,_rate,_note,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('exchange_rate_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_exchange_rate_select(_id	integer)
RETURNS SETOF exchange_rate
AS $$
BEGIN
	RETURN QUERY SELECT * FROM exchange_rate WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_exchange_rate_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_source_currency	integer DEFAULT null,
	_id_target_currency	integer DEFAULT null,
	_from_timeFrom	timestamp without time zone DEFAULT null,
	_from_timeTo	timestamp without time zone DEFAULT null,
	_to_timeFrom	timestamp without time zone DEFAULT null,
	_to_timeTo	timestamp without time zone DEFAULT null,
	_rate	numeric DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF exchange_rate
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM exchange_rate WHERE
	 	(_id is null OR id = _id)	
	AND (_id_source_currency is null OR id_source_currency = _id_source_currency)	
	AND (_id_target_currency is null OR id_target_currency = _id_target_currency)	
	AND (_from_timeFrom is null OR from_time >= _from_timeFrom)
	AND	(_from_timeTo is null OR from_time <= _from_timeTo)	
	AND (_to_timeFrom is null OR to_time >= _to_timeFrom)
	AND	(_to_timeTo is null OR to_time <= _to_timeTo)	
	AND (_rate is null OR rate = _rate)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_exchange_rate_count (
	_id	integer DEFAULT null,
	_id_source_currency	integer DEFAULT null,
	_id_target_currency	integer DEFAULT null,
	_from_timeFrom	timestamp without time zone DEFAULT null,
	_from_timeTo	timestamp without time zone DEFAULT null,
	_to_timeFrom	timestamp without time zone DEFAULT null,
	_to_timeTo	timestamp without time zone DEFAULT null,
	_rate	numeric DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM exchange_rate WHERE
	 	(_id is null OR id = _id)	
	AND (_id_source_currency is null OR id_source_currency = _id_source_currency)	
	AND (_id_target_currency is null OR id_target_currency = _id_target_currency)	
	AND (_from_timeFrom is null OR from_time >= _from_timeFrom)
	AND	(_from_timeTo is null OR from_time <= _from_timeTo)	
	AND (_to_timeFrom is null OR to_time >= _to_timeFrom)
	AND	(_to_timeTo is null OR to_time <= _to_timeTo)	
	AND (_rate is null OR rate = _rate)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_exchange_rate_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM exchange_rate
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_exchange_rate_update 
(	_id integer,
	_id_source_currency integer,
	_id_target_currency integer,
	_from_time timestamp without time zone,
	_to_time timestamp without time zone,
	_rate numeric,
	_note character varying,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE exchange_rate 
	SET 		id_source_currency = _id_source_currency,
		id_target_currency = _id_target_currency,
		from_time = _from_time,
		to_time = _to_time,
		rate = _rate,
		note = _note,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of exchange_rate

-- file
CREATE OR REPLACE FUNCTION sp_file_insert(
	_id_document	integer,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_ext	character varying(10) DEFAULT null,
	_media_type	integer DEFAULT null,
	_save_mode	integer DEFAULT null,
	_file_path	character varying DEFAULT null,
	_content	bytea DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO file(id_document,name,code,description,ext,media_type,save_mode,file_path,content,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_document,_name,_code,_description,_ext,_media_type,_save_mode,_file_path,_content,_status,_is_draft,_notification,_creator,_creator,null);
RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_file_select(_id_document	integer)
RETURNS SETOF file
AS $$
BEGIN
	RETURN QUERY SELECT * FROM file WHERE id_document = _id_document;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_file_selectList (
	_limit	integer,
	_offset integer,
	_id_document	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_ext	character varying(10) DEFAULT null,
	_media_type	integer DEFAULT null,
	_save_mode	integer DEFAULT null,
	_file_path	character varying DEFAULT null,
	_content	bytea DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF file
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM file WHERE
	 	(_id_document is null OR id_document = _id_document)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_ext is null OR ext = _ext)	
	AND (_media_type is null OR media_type = _media_type)	
	AND (_save_mode is null OR save_mode = _save_mode)	
	AND (_file_path is null OR file_path = _file_path)	
	AND (_content is null OR content = _content)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_file_count (
	_id_document	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_ext	character varying(10) DEFAULT null,
	_media_type	integer DEFAULT null,
	_save_mode	integer DEFAULT null,
	_file_path	character varying DEFAULT null,
	_content	bytea DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM file WHERE
	 	(_id_document is null OR id_document = _id_document)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_ext is null OR ext = _ext)	
	AND (_media_type is null OR media_type = _media_type)	
	AND (_save_mode is null OR save_mode = _save_mode)	
	AND (_file_path is null OR file_path = _file_path)	
	AND (_content is null OR content = _content)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_file_delete(_id_document integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM file
 		WHERE "id_document" = _id_document;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_file_update 
(	_id_document integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_ext character varying(10),
	_media_type integer,
	_save_mode integer,
	_file_path character varying,
	_content bytea,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE file 
	SET 		name = _name,
		code = _code,
		description = _description,
		ext = _ext,
		media_type = _media_type,
		save_mode = _save_mode,
		file_path = _file_path,
		content = _content,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id_document = _id_document;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of file

-- industry
CREATE OR REPLACE FUNCTION sp_industry_insert(
	_name	character varying(255),
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO industry(name,code,description,id_parent,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_name,_code,_description,_id_parent,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('industry_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_industry_select(_id	integer)
RETURNS SETOF industry
AS $$
BEGIN
	RETURN QUERY SELECT * FROM industry WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_industry_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF industry
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM industry WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_industry_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM industry WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_industry_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM industry
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_industry_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_id_parent integer,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE industry 
	SET 		name = _name,
		code = _code,
		description = _description,
		id_parent = _id_parent,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of industry

-- introduction
CREATE OR REPLACE FUNCTION sp_introduction_insert(
	_id_company	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_id_intro_company	integer DEFAULT null,
	_id_intro_person	integer DEFAULT null,
	_type	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_intro_date	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO introduction(id_company,id_project,id_intro_company,id_intro_person,type,note,intro_date,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_company,_id_project,_id_intro_company,_id_intro_person,_type,_note,_intro_date,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('introduction_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_introduction_select(_id	integer)
RETURNS SETOF introduction
AS $$
BEGIN
	RETURN QUERY SELECT * FROM introduction WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_introduction_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_id_intro_company	integer DEFAULT null,
	_id_intro_person	integer DEFAULT null,
	_type	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_intro_dateFrom	timestamp without time zone DEFAULT null,
	_intro_dateTo	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF introduction
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM introduction WHERE
	 	(_id is null OR id = _id)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_id_intro_company is null OR id_intro_company = _id_intro_company)	
	AND (_id_intro_person is null OR id_intro_person = _id_intro_person)	
	AND (_type is null OR type = _type)	
	AND (_note is null OR note = _note)	
	AND (_intro_dateFrom is null OR intro_date >= _intro_dateFrom)
	AND	(_intro_dateTo is null OR intro_date <= _intro_dateTo)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_introduction_count (
	_id	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_id_intro_company	integer DEFAULT null,
	_id_intro_person	integer DEFAULT null,
	_type	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_intro_dateFrom	timestamp without time zone DEFAULT null,
	_intro_dateTo	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM introduction WHERE
	 	(_id is null OR id = _id)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_id_intro_company is null OR id_intro_company = _id_intro_company)	
	AND (_id_intro_person is null OR id_intro_person = _id_intro_person)	
	AND (_type is null OR type = _type)	
	AND (_note is null OR note = _note)	
	AND (_intro_dateFrom is null OR intro_date >= _intro_dateFrom)
	AND	(_intro_dateTo is null OR intro_date <= _intro_dateTo)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_introduction_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM introduction
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_introduction_update 
(	_id integer,
	_id_company integer,
	_id_project integer,
	_id_intro_company integer,
	_id_intro_person integer,
	_type integer,
	_note character varying,
	_intro_date timestamp without time zone,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE introduction 
	SET 		id_company = _id_company,
		id_project = _id_project,
		id_intro_company = _id_intro_company,
		id_intro_person = _id_intro_person,
		type = _type,
		note = _note,
		intro_date = _intro_date,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of introduction

-- log
CREATE OR REPLACE FUNCTION sp_log_insert(
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_action	integer DEFAULT null,
	_id_user	character varying(20) DEFAULT null,
	_transaction	integer DEFAULT null,
	_log_time	timestamp without time zone DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO log(object_type,object_id,action,id_user,transaction,log_time)
	VALUES(_object_type,_object_id,_action,_id_user,_transaction,_log_time);
RETURN currval('log_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_log_select(_id	integer)
RETURNS SETOF log
AS $$
BEGIN
	RETURN QUERY SELECT * FROM log WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_log_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_action	integer DEFAULT null,
	_id_user	character varying(20) DEFAULT null,
	_transaction	integer DEFAULT null,
	_log_timeFrom	timestamp without time zone DEFAULT null,
	_log_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF log
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM log WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_action is null OR action = _action)	
	AND (_id_user is null OR id_user = _id_user)	
	AND (_transaction is null OR transaction = _transaction)	
	AND (_log_timeFrom is null OR log_time >= _log_timeFrom)
	AND	(_log_timeTo is null OR log_time <= _log_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_log_count (
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	integer DEFAULT null,
	_action	integer DEFAULT null,
	_id_user	character varying(20) DEFAULT null,
	_transaction	integer DEFAULT null,
	_log_timeFrom	timestamp without time zone DEFAULT null,
	_log_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM log WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_action is null OR action = _action)	
	AND (_id_user is null OR id_user = _id_user)	
	AND (_transaction is null OR transaction = _transaction)	
	AND (_log_timeFrom is null OR log_time >= _log_timeFrom)
	AND	(_log_timeTo is null OR log_time <= _log_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_log_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM log
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_log_update 
(	_id integer,
	_object_type character varying(50),
	_object_id integer,
	_action integer,
	_id_user character varying(20),
	_transaction integer,
	_log_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE log 
	SET 		object_type = _object_type,
		object_id = _object_id,
		action = _action,
		id_user = _id_user,
		transaction = _transaction,
		log_time = _log_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of log

-- log_item
CREATE OR REPLACE FUNCTION sp_log_item_insert(
	_id_log	integer DEFAULT null,
	_field	character varying(50) DEFAULT null,
	_data_type	character varying(50) DEFAULT null,
	_old_value	character varying DEFAULT null,
	_new_value	character varying DEFAULT null,
	_log_time	timestamp without time zone DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO log_item(id_log,field,data_type,old_value,new_value,log_time)
	VALUES(_id_log,_field,_data_type,_old_value,_new_value,_log_time);
RETURN currval('log_item_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_log_item_select(_id	integer)
RETURNS SETOF log_item
AS $$
BEGIN
	RETURN QUERY SELECT * FROM log_item WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_log_item_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_log	integer DEFAULT null,
	_field	character varying(50) DEFAULT null,
	_data_type	character varying(50) DEFAULT null,
	_old_value	character varying DEFAULT null,
	_new_value	character varying DEFAULT null,
	_log_timeFrom	timestamp without time zone DEFAULT null,
	_log_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF log_item
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM log_item WHERE
	 	(_id is null OR id = _id)	
	AND (_id_log is null OR id_log = _id_log)	
	AND (_field is null OR field = _field)	
	AND (_data_type is null OR data_type = _data_type)	
	AND (_old_value is null OR old_value = _old_value)	
	AND (_new_value is null OR new_value = _new_value)	
	AND (_log_timeFrom is null OR log_time >= _log_timeFrom)
	AND	(_log_timeTo is null OR log_time <= _log_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_log_item_count (
	_id	integer DEFAULT null,
	_id_log	integer DEFAULT null,
	_field	character varying(50) DEFAULT null,
	_data_type	character varying(50) DEFAULT null,
	_old_value	character varying DEFAULT null,
	_new_value	character varying DEFAULT null,
	_log_timeFrom	timestamp without time zone DEFAULT null,
	_log_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM log_item WHERE
	 	(_id is null OR id = _id)	
	AND (_id_log is null OR id_log = _id_log)	
	AND (_field is null OR field = _field)	
	AND (_data_type is null OR data_type = _data_type)	
	AND (_old_value is null OR old_value = _old_value)	
	AND (_new_value is null OR new_value = _new_value)	
	AND (_log_timeFrom is null OR log_time >= _log_timeFrom)
	AND	(_log_timeTo is null OR log_time <= _log_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_log_item_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM log_item
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_log_item_update 
(	_id integer,
	_id_log integer,
	_field character varying(50),
	_data_type character varying(50),
	_old_value character varying,
	_new_value character varying,
	_log_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE log_item 
	SET 		id_log = _id_log,
		field = _field,
		data_type = _data_type,
		old_value = _old_value,
		new_value = _new_value,
		log_time = _log_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of log_item

-- online_conference
CREATE OR REPLACE FUNCTION sp_online_conference_insert(
	_object_type	character varying(50) DEFAULT null,
	_object_id	character varying(50) DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_start_time	timestamp without time zone DEFAULT null,
	_id_employee	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO online_conference(object_type,object_id,name,code,description,start_time,id_employee,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_object_type,_object_id,_name,_code,_description,_start_time,_id_employee,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('online_conference_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_online_conference_select(_id	integer)
RETURNS SETOF online_conference
AS $$
BEGIN
	RETURN QUERY SELECT * FROM online_conference WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_online_conference_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	character varying(50) DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_start_timeFrom	timestamp without time zone DEFAULT null,
	_start_timeTo	timestamp without time zone DEFAULT null,
	_id_employee	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF online_conference
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM online_conference WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_start_timeFrom is null OR start_time >= _start_timeFrom)
	AND	(_start_timeTo is null OR start_time <= _start_timeTo)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_online_conference_count (
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	character varying(50) DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_start_timeFrom	timestamp without time zone DEFAULT null,
	_start_timeTo	timestamp without time zone DEFAULT null,
	_id_employee	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM online_conference WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_start_timeFrom is null OR start_time >= _start_timeFrom)
	AND	(_start_timeTo is null OR start_time <= _start_timeTo)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_online_conference_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM online_conference
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_online_conference_update 
(	_id integer,
	_object_type character varying(50),
	_object_id character varying(50),
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_start_time timestamp without time zone,
	_id_employee integer,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE online_conference 
	SET 		object_type = _object_type,
		object_id = _object_id,
		name = _name,
		code = _code,
		description = _description,
		start_time = _start_time,
		id_employee = _id_employee,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of online_conference

-- online_conference_attendee
CREATE OR REPLACE FUNCTION sp_online_conference_attendee_insert(
	_id_conference	integer,
	_id_employee	integer,
	_required	boolean DEFAULT null,
	_confirmed	integer DEFAULT null,
	_confirmation_time	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO online_conference_attendee(id_conference,id_employee,required,confirmed,confirmation_time,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_conference,_id_employee,_required,_confirmed,_confirmation_time,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('online_conference_attendee_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_online_conference_attendee_select(_id	integer)
RETURNS SETOF online_conference_attendee
AS $$
BEGIN
	RETURN QUERY SELECT * FROM online_conference_attendee WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_online_conference_attendee_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_conference	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_required	boolean DEFAULT null,
	_confirmed	integer DEFAULT null,
	_confirmation_timeFrom	timestamp without time zone DEFAULT null,
	_confirmation_timeTo	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF online_conference_attendee
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM online_conference_attendee WHERE
	 	(_id is null OR id = _id)	
	AND (_id_conference is null OR id_conference = _id_conference)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_required is null OR required = _required)	
	AND (_confirmed is null OR confirmed = _confirmed)	
	AND (_confirmation_timeFrom is null OR confirmation_time >= _confirmation_timeFrom)
	AND	(_confirmation_timeTo is null OR confirmation_time <= _confirmation_timeTo)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_online_conference_attendee_count (
	_id	integer DEFAULT null,
	_id_conference	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_required	boolean DEFAULT null,
	_confirmed	integer DEFAULT null,
	_confirmation_timeFrom	timestamp without time zone DEFAULT null,
	_confirmation_timeTo	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM online_conference_attendee WHERE
	 	(_id is null OR id = _id)	
	AND (_id_conference is null OR id_conference = _id_conference)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_required is null OR required = _required)	
	AND (_confirmed is null OR confirmed = _confirmed)	
	AND (_confirmation_timeFrom is null OR confirmation_time >= _confirmation_timeFrom)
	AND	(_confirmation_timeTo is null OR confirmation_time <= _confirmation_timeTo)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_online_conference_attendee_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM online_conference_attendee
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_online_conference_attendee_update 
(	_id integer,
	_id_conference integer,
	_id_employee integer,
	_required boolean,
	_confirmed integer,
	_confirmation_time timestamp without time zone,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE online_conference_attendee 
	SET 		id_conference = _id_conference,
		id_employee = _id_employee,
		required = _required,
		confirmed = _confirmed,
		confirmation_time = _confirmation_time,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of online_conference_attendee

-- payable
CREATE OR REPLACE FUNCTION sp_payable_insert(
	_type	integer DEFAULT null,
	_id_task	integer DEFAULT null,
	_id_purchase	integer DEFAULT null,
	_plan_date	timestamp without time zone DEFAULT null,
	_real_date	timestamp without time zone DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_value	numeric DEFAULT null,
	_need_approval	boolean DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO payable(type,id_task,id_purchase,plan_date,real_date,name,code,description,value,need_approval,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_type,_id_task,_id_purchase,_plan_date,_real_date,_name,_code,_description,_value,_need_approval,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('payable_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_payable_select(_id	integer)
RETURNS SETOF payable
AS $$
BEGIN
	RETURN QUERY SELECT * FROM payable WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_payable_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_type	integer DEFAULT null,
	_id_task	integer DEFAULT null,
	_id_purchase	integer DEFAULT null,
	_plan_dateFrom	timestamp without time zone DEFAULT null,
	_plan_dateTo	timestamp without time zone DEFAULT null,
	_real_dateFrom	timestamp without time zone DEFAULT null,
	_real_dateTo	timestamp without time zone DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_value	numeric DEFAULT null,
	_need_approval	boolean DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF payable
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM payable WHERE
	 	(_id is null OR id = _id)	
	AND (_type is null OR type = _type)	
	AND (_id_task is null OR id_task = _id_task)	
	AND (_id_purchase is null OR id_purchase = _id_purchase)	
	AND (_plan_dateFrom is null OR plan_date >= _plan_dateFrom)
	AND	(_plan_dateTo is null OR plan_date <= _plan_dateTo)	
	AND (_real_dateFrom is null OR real_date >= _real_dateFrom)
	AND	(_real_dateTo is null OR real_date <= _real_dateTo)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_value is null OR value = _value)	
	AND (_need_approval is null OR need_approval = _need_approval)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_payable_count (
	_id	integer DEFAULT null,
	_type	integer DEFAULT null,
	_id_task	integer DEFAULT null,
	_id_purchase	integer DEFAULT null,
	_plan_dateFrom	timestamp without time zone DEFAULT null,
	_plan_dateTo	timestamp without time zone DEFAULT null,
	_real_dateFrom	timestamp without time zone DEFAULT null,
	_real_dateTo	timestamp without time zone DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_value	numeric DEFAULT null,
	_need_approval	boolean DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM payable WHERE
	 	(_id is null OR id = _id)	
	AND (_type is null OR type = _type)	
	AND (_id_task is null OR id_task = _id_task)	
	AND (_id_purchase is null OR id_purchase = _id_purchase)	
	AND (_plan_dateFrom is null OR plan_date >= _plan_dateFrom)
	AND	(_plan_dateTo is null OR plan_date <= _plan_dateTo)	
	AND (_real_dateFrom is null OR real_date >= _real_dateFrom)
	AND	(_real_dateTo is null OR real_date <= _real_dateTo)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_value is null OR value = _value)	
	AND (_need_approval is null OR need_approval = _need_approval)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_payable_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM payable
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_payable_update 
(	_id integer,
	_type integer,
	_id_task integer,
	_id_purchase integer,
	_plan_date timestamp without time zone,
	_real_date timestamp without time zone,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_value numeric,
	_need_approval boolean,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE payable 
	SET 		type = _type,
		id_task = _id_task,
		id_purchase = _id_purchase,
		plan_date = _plan_date,
		real_date = _real_date,
		name = _name,
		code = _code,
		description = _description,
		value = _value,
		need_approval = _need_approval,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of payable

-- person
CREATE OR REPLACE FUNCTION sp_person_select(_id	integer)
RETURNS SETOF person
AS $$
BEGIN
	RETURN QUERY SELECT * FROM person WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_person_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM person
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_person_insert(
	_is_employee	boolean DEFAULT null,
	_first_name	character varying(25) DEFAULT null,
	_last_name	character varying(25) DEFAULT null,
	_mobile	character varying(20) DEFAULT null,
	_company_phone	character varying(25) DEFAULT null,
	_home_phone	character varying(25) DEFAULT null,
	_company_email	character varying(255) DEFAULT null,
	_personal_email	character varying(255) DEFAULT null,
	_address	character varying(255) DEFAULT null,
	_birthdate	timestamp without time zone DEFAULT null,
	_identity_no	character varying(25) DEFAULT null,
	_gender	boolean DEFAULT null,
	_description	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null,
	_middle_name	character varying(25) DEFAULT null,
	_id_employee	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO person(is_employee,first_name,last_name,mobile,company_phone,home_phone,company_email,personal_email,address,birthdate,identity_no,gender,description,status,is_draft,notification,creator,updator,deletion_time,middle_name,id_employee)
	VALUES(_is_employee,_first_name,_last_name,_mobile,_company_phone,_home_phone,_company_email,_personal_email,_address,_birthdate,_identity_no,_gender,_description,_status,_is_draft,_notification,_creator,_creator,null,_middle_name,_id_employee);
RETURN currval('person_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_person_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_is_employee	boolean DEFAULT null,
	_first_name	character varying(25) DEFAULT null,
	_last_name	character varying(25) DEFAULT null,
	_mobile	character varying(20) DEFAULT null,
	_company_phone	character varying(25) DEFAULT null,
	_home_phone	character varying(25) DEFAULT null,
	_company_email	character varying(255) DEFAULT null,
	_personal_email	character varying(255) DEFAULT null,
	_address	character varying(255) DEFAULT null,
	_birthdateFrom	timestamp without time zone DEFAULT null,
	_birthdateTo	timestamp without time zone DEFAULT null,
	_identity_no	character varying(25) DEFAULT null,
	_gender	boolean DEFAULT null,
	_description	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_middle_name	character varying(25) DEFAULT null,
	_id_employee	integer DEFAULT null
)
RETURNS SETOF person
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM person WHERE
	 	(_id is null OR id = _id)	
	AND (_is_employee is null OR is_employee = _is_employee)	
	AND (_first_name is null OR first_name = _first_name)	
	AND (_last_name is null OR last_name = _last_name)	
	AND (_mobile is null OR mobile = _mobile)	
	AND (_company_phone is null OR company_phone = _company_phone)	
	AND (_home_phone is null OR home_phone = _home_phone)	
	AND (_company_email is null OR company_email = _company_email)	
	AND (_personal_email is null OR personal_email = _personal_email)	
	AND (_address is null OR address = _address)	
	AND (_birthdateFrom is null OR birthdate >= _birthdateFrom)
	AND	(_birthdateTo is null OR birthdate <= _birthdateTo)	
	AND (_identity_no is null OR identity_no = _identity_no)	
	AND (_gender is null OR gender = _gender)	
	AND (_description is null OR description = _description)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_middle_name is null OR middle_name = _middle_name)	
	AND (_id_employee is null OR id_employee = _id_employee)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_person_count (
	_id	integer DEFAULT null,
	_is_employee	boolean DEFAULT null,
	_first_name	character varying(25) DEFAULT null,
	_last_name	character varying(25) DEFAULT null,
	_mobile	character varying(20) DEFAULT null,
	_company_phone	character varying(25) DEFAULT null,
	_home_phone	character varying(25) DEFAULT null,
	_company_email	character varying(255) DEFAULT null,
	_personal_email	character varying(255) DEFAULT null,
	_address	character varying(255) DEFAULT null,
	_birthdateFrom	timestamp without time zone DEFAULT null,
	_birthdateTo	timestamp without time zone DEFAULT null,
	_identity_no	character varying(25) DEFAULT null,
	_gender	boolean DEFAULT null,
	_description	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_middle_name	character varying(25) DEFAULT null,
	_id_employee	integer DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM person WHERE
	 	(_id is null OR id = _id)	
	AND (_is_employee is null OR is_employee = _is_employee)	
	AND (_first_name is null OR first_name = _first_name)	
	AND (_last_name is null OR last_name = _last_name)	
	AND (_mobile is null OR mobile = _mobile)	
	AND (_company_phone is null OR company_phone = _company_phone)	
	AND (_home_phone is null OR home_phone = _home_phone)	
	AND (_company_email is null OR company_email = _company_email)	
	AND (_personal_email is null OR personal_email = _personal_email)	
	AND (_address is null OR address = _address)	
	AND (_birthdateFrom is null OR birthdate >= _birthdateFrom)
	AND	(_birthdateTo is null OR birthdate <= _birthdateTo)	
	AND (_identity_no is null OR identity_no = _identity_no)	
	AND (_gender is null OR gender = _gender)	
	AND (_description is null OR description = _description)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_middle_name is null OR middle_name = _middle_name)	
	AND (_id_employee is null OR id_employee = _id_employee);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_person_update 
(	_id integer,
	_is_employee boolean,
	_first_name character varying(25),
	_last_name character varying(25),
	_mobile character varying(20),
	_company_phone character varying(25),
	_home_phone character varying(25),
	_company_email character varying(255),
	_personal_email character varying(255),
	_address character varying(255),
	_birthdate timestamp without time zone,
	_identity_no character varying(25),
	_gender boolean,
	_description character varying,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone,
	_middle_name character varying(25),
	_id_employee integer  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE person 
	SET 		is_employee = _is_employee,
		first_name = _first_name,
		last_name = _last_name,
		mobile = _mobile,
		company_phone = _company_phone,
		home_phone = _home_phone,
		company_email = _company_email,
		personal_email = _personal_email,
		address = _address,
		birthdate = _birthdate,
		identity_no = _identity_no,
		gender = _gender,
		description = _description,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time,
		middle_name = _middle_name,
		id_employee = _id_employee
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of person

-- phase

CREATE OR REPLACE FUNCTION sp_phase_insert(
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_project	integer DEFAULT null,
	_status	integer DEFAULT null,
	_plan_start_date	timestamp without time zone DEFAULT null,
	_plan_end_date	timestamp without time zone DEFAULT null,
	_real_start_date	timestamp without time zone DEFAULT null,
	_real_end_date	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_remark	character varying DEFAULT null,
	_index	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO phase(name,code,description,id_project,status,plan_start_date,plan_end_date,real_start_date,real_end_date,creator,updator,deletion_time,remark,index)
	VALUES(_name,_code,_description,_id_project,_status,_plan_start_date,_plan_end_date,_real_start_date,_real_end_date,_creator,_creator,null,_remark,_index);
RETURN currval('phase_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_phase_select(_id	integer)
RETURNS SETOF phase
AS $$
BEGIN
	RETURN QUERY SELECT * FROM phase WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_phase_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_project	integer DEFAULT null,
	_status	integer DEFAULT null,
	_plan_start_dateFrom	timestamp without time zone DEFAULT null,
	_plan_start_dateTo	timestamp without time zone DEFAULT null,
	_plan_end_dateFrom	timestamp without time zone DEFAULT null,
	_plan_end_dateTo	timestamp without time zone DEFAULT null,
	_real_start_dateFrom	timestamp without time zone DEFAULT null,
	_real_start_dateTo	timestamp without time zone DEFAULT null,
	_real_end_dateFrom	timestamp without time zone DEFAULT null,
	_real_end_dateTo	timestamp without time zone DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_remark	character varying DEFAULT null,
	_index	integer DEFAULT null
)
RETURNS SETOF phase
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM phase WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_status is null OR status = _status)	
	AND (_plan_start_dateFrom is null OR plan_start_date >= _plan_start_dateFrom)
	AND	(_plan_start_dateTo is null OR plan_start_date <= _plan_start_dateTo)	
	AND (_plan_end_dateFrom is null OR plan_end_date >= _plan_end_dateFrom)
	AND	(_plan_end_dateTo is null OR plan_end_date <= _plan_end_dateTo)	
	AND (_real_start_dateFrom is null OR real_start_date >= _real_start_dateFrom)
	AND	(_real_start_dateTo is null OR real_start_date <= _real_start_dateTo)	
	AND (_real_end_dateFrom is null OR real_end_date >= _real_end_dateFrom)
	AND	(_real_end_dateTo is null OR real_end_date <= _real_end_dateTo)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_remark is null OR remark = _remark)	
	AND (_index is null OR index = _index)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_phase_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_project	integer DEFAULT null,
	_status	integer DEFAULT null,
	_plan_start_dateFrom	timestamp without time zone DEFAULT null,
	_plan_start_dateTo	timestamp without time zone DEFAULT null,
	_plan_end_dateFrom	timestamp without time zone DEFAULT null,
	_plan_end_dateTo	timestamp without time zone DEFAULT null,
	_real_start_dateFrom	timestamp without time zone DEFAULT null,
	_real_start_dateTo	timestamp without time zone DEFAULT null,
	_real_end_dateFrom	timestamp without time zone DEFAULT null,
	_real_end_dateTo	timestamp without time zone DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_remark	character varying DEFAULT null,
	_index	integer DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM phase WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_status is null OR status = _status)	
	AND (_plan_start_dateFrom is null OR plan_start_date >= _plan_start_dateFrom)
	AND	(_plan_start_dateTo is null OR plan_start_date <= _plan_start_dateTo)	
	AND (_plan_end_dateFrom is null OR plan_end_date >= _plan_end_dateFrom)
	AND	(_plan_end_dateTo is null OR plan_end_date <= _plan_end_dateTo)	
	AND (_real_start_dateFrom is null OR real_start_date >= _real_start_dateFrom)
	AND	(_real_start_dateTo is null OR real_start_date <= _real_start_dateTo)	
	AND (_real_end_dateFrom is null OR real_end_date >= _real_end_dateFrom)
	AND	(_real_end_dateTo is null OR real_end_date <= _real_end_dateTo)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_remark is null OR remark = _remark)	
	AND (_index is null OR index = _index);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_phase_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM phase
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_phase_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_id_project integer,
	_status integer,
	_plan_start_date timestamp without time zone,
	_plan_end_date timestamp without time zone,
	_real_start_date timestamp without time zone,
	_real_end_date timestamp without time zone,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone,
	_remark character varying,
	_index integer  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE phase 
	SET 		name = _name,
		code = _code,
		description = _description,
		id_project = _id_project,
		status = _status,
		plan_start_date = _plan_start_date,
		plan_end_date = _plan_end_date,
		real_start_date = _real_start_date,
		real_end_date = _real_end_date,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time,
		remark = _remark,
		index = _index
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of phase

-- place
CREATE OR REPLACE FUNCTION sp_place_insert(
	_id_type	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO place(id_type,name,code,description,id_parent,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_type,_name,_code,_description,_id_parent,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('place_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_place_select(_id	integer)
RETURNS SETOF place
AS $$
BEGIN
	RETURN QUERY SELECT * FROM place WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_place_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_type	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF place
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM place WHERE
	 	(_id is null OR id = _id)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_place_count (
	_id	integer DEFAULT null,
	_id_type	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM place WHERE
	 	(_id is null OR id = _id)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_place_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM place
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_place_update 
(	_id integer,
	_id_type integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_id_parent integer,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE place 
	SET 		id_type = _id_type,
		name = _name,
		code = _code,
		description = _description,
		id_parent = _id_parent,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of place

-- place_type
CREATE OR REPLACE FUNCTION sp_place_type_insert(
	_name	character varying(255),
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO place_type(name,code,description,status,creator,updator,deletion_time)
	VALUES(_name,_code,_description,_status,_creator,_creator,null);
RETURN currval('place_type_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_place_type_select(_id	integer)
RETURNS SETOF place_type
AS $$
BEGIN
	RETURN QUERY SELECT * FROM place_type WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_place_type_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF place_type
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM place_type WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_status is null OR status = _status)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_place_type_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM place_type WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_status is null OR status = _status)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_place_type_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM place_type
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_place_type_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_status integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE place_type 
	SET 		name = _name,
		code = _code,
		description = _description,
		status = _status,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of place_type

-- position
CREATE OR REPLACE FUNCTION sp_position_insert(
	_name	character varying(255),
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO position(name,code,description,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_name,_code,_description,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('position_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_position_select(_id	integer)
RETURNS SETOF "position"
AS $$
BEGIN
	RETURN QUERY SELECT * FROM position WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_position_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF "position"
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM position WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_position_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM position WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_position_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM position
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_position_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE position 
	SET 		name = _name,
		code = _code,
		description = _description,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of position

-- position_right
CREATE OR REPLACE FUNCTION sp_position_right_insert(
	_id_position	integer,
	_right_code	integer,
	_object_type	character varying(50) DEFAULT null,
	_quota	numeric DEFAULT null,
	_note	character varying(511) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO position_right(id_position,right_code,object_type,quota,note,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_position,_right_code,_object_type,_quota,_note,_status,_is_draft,_notification,_creator,_creator,null);
RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_position_right_select(_id_position	integer , _right_code	integer)
RETURNS SETOF position_right
AS $$
BEGIN
	RETURN QUERY SELECT * FROM position_right WHERE id_position = _id_position AND right_code = _right_code;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_position_right_selectList (
	_limit	integer,
	_offset integer,
	_id_position	integer DEFAULT null,
	_right_code	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_quota	numeric DEFAULT null,
	_note	character varying(511) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF position_right
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM position_right WHERE
	 	(_id_position is null OR id_position = _id_position)	
	AND (_right_code is null OR right_code = _right_code)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_quota is null OR quota = _quota)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_position_right_count (
	_id_position	integer DEFAULT null,
	_right_code	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_quota	numeric DEFAULT null,
	_note	character varying(511) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM position_right WHERE
	 	(_id_position is null OR id_position = _id_position)	
	AND (_right_code is null OR right_code = _right_code)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_quota is null OR quota = _quota)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_position_right_delete(_id_position integer,	_right_code integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM position_right
 		WHERE "id_position" = _id_position AND "right_code" = _right_code;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_position_right_update 
(	_id_position integer,
	_right_code integer,
	_object_type character varying(50),
	_quota numeric,
	_note character varying(511),
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE position_right 
	SET 		object_type = _object_type,
		quota = _quota,
		note = _note,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id_position = _id_position and right_code = _right_code;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of position_right

-- price
CREATE OR REPLACE FUNCTION sp_price_insert(
	_id_product	integer DEFAULT null,
	_id_unit	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_price	numeric DEFAULT null,
	_from_date	timestamp without time zone DEFAULT null,
	_to_date	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO price(id_product,id_unit,note,price,from_date,to_date,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_product,_id_unit,_note,_price,_from_date,_to_date,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('price_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_price_select(_id	integer)
RETURNS SETOF price
AS $$
BEGIN
	RETURN QUERY SELECT * FROM price WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_price_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_product	integer DEFAULT null,
	_id_unit	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_price	numeric DEFAULT null,
	_from_dateFrom	timestamp without time zone DEFAULT null,
	_from_dateTo	timestamp without time zone DEFAULT null,
	_to_dateFrom	timestamp without time zone DEFAULT null,
	_to_dateTo	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF price
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM price WHERE
	 	(_id is null OR id = _id)	
	AND (_id_product is null OR id_product = _id_product)	
	AND (_id_unit is null OR id_unit = _id_unit)	
	AND (_note is null OR note = _note)	
	AND (_price is null OR price = _price)	
	AND (_from_dateFrom is null OR from_date >= _from_dateFrom)
	AND	(_from_dateTo is null OR from_date <= _from_dateTo)	
	AND (_to_dateFrom is null OR to_date >= _to_dateFrom)
	AND	(_to_dateTo is null OR to_date <= _to_dateTo)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_price_count (
	_id	integer DEFAULT null,
	_id_product	integer DEFAULT null,
	_id_unit	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_price	numeric DEFAULT null,
	_from_dateFrom	timestamp without time zone DEFAULT null,
	_from_dateTo	timestamp without time zone DEFAULT null,
	_to_dateFrom	timestamp without time zone DEFAULT null,
	_to_dateTo	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM price WHERE
	 	(_id is null OR id = _id)	
	AND (_id_product is null OR id_product = _id_product)	
	AND (_id_unit is null OR id_unit = _id_unit)	
	AND (_note is null OR note = _note)	
	AND (_price is null OR price = _price)	
	AND (_from_dateFrom is null OR from_date >= _from_dateFrom)
	AND	(_from_dateTo is null OR from_date <= _from_dateTo)	
	AND (_to_dateFrom is null OR to_date >= _to_dateFrom)
	AND	(_to_dateTo is null OR to_date <= _to_dateTo)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_price_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM price
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_price_update 
(	_id integer,
	_id_product integer,
	_id_unit integer,
	_note character varying,
	_price numeric,
	_from_date timestamp without time zone,
	_to_date timestamp without time zone,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE price 
	SET 		id_product = _id_product,
		id_unit = _id_unit,
		note = _note,
		price = _price,
		from_date = _from_date,
		to_date = _to_date,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of price

-- product
CREATE OR REPLACE FUNCTION sp_product_insert(
	_short_name	character varying(25) DEFAULT null,
	_full_name	character varying(255) DEFAULT null,
	_foreign_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_default_unit	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO product(short_name,full_name,foreign_name,code,description,id_default_unit,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_short_name,_full_name,_foreign_name,_code,_description,_id_default_unit,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('product_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_product_select(_id	integer)
RETURNS SETOF product
AS $$
BEGIN
	RETURN QUERY SELECT * FROM product WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_product_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_short_name	character varying(25) DEFAULT null,
	_full_name	character varying(255) DEFAULT null,
	_foreign_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_default_unit	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF product
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM product WHERE
	 	(_id is null OR id = _id)	
	AND (_short_name is null OR short_name = _short_name)	
	AND (_full_name is null OR full_name = _full_name)	
	AND (_foreign_name is null OR foreign_name = _foreign_name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_default_unit is null OR id_default_unit = _id_default_unit)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_product_count (
	_id	integer DEFAULT null,
	_short_name	character varying(25) DEFAULT null,
	_full_name	character varying(255) DEFAULT null,
	_foreign_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_default_unit	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM product WHERE
	 	(_id is null OR id = _id)	
	AND (_short_name is null OR short_name = _short_name)	
	AND (_full_name is null OR full_name = _full_name)	
	AND (_foreign_name is null OR foreign_name = _foreign_name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_default_unit is null OR id_default_unit = _id_default_unit)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_product_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM product
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_product_update 
(	_id integer,
	_short_name character varying(25),
	_full_name character varying(255),
	_foreign_name character varying(255),
	_code character varying(20),
	_description character varying,
	_id_default_unit integer,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE product 
	SET 		short_name = _short_name,
		full_name = _full_name,
		foreign_name = _foreign_name,
		code = _code,
		description = _description,
		id_default_unit = _id_default_unit,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of product

-- product_category
CREATE OR REPLACE FUNCTION sp_product_category_insert(
	_name	character varying(255),
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO product_category(name,code,description,id_parent,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_name,_code,_description,_id_parent,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('product_category_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_product_category_select(_id	integer)
RETURNS SETOF product_category
AS $$
BEGIN
	RETURN QUERY SELECT * FROM product_category WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_product_category_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF product_category
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM product_category WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_product_category_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM product_category WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_product_category_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM product_category
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_product_category_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_id_parent integer,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE product_category 
	SET 		name = _name,
		code = _code,
		description = _description,
		id_parent = _id_parent,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of product_category

-- product_product_category
CREATE OR REPLACE FUNCTION sp_product_product_category_insert(
	_id_product	integer,
	_id_category	integer,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO product_product_category(id_product,id_category,note,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_product,_id_category,_note,_status,_is_draft,_notification,_creator,_creator,null);
RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_product_product_category_select(_id_product	integer , _id_category	integer)
RETURNS SETOF product_product_category
AS $$
BEGIN
	RETURN QUERY SELECT * FROM product_product_category WHERE id_product = _id_product AND id_category = _id_category;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_product_product_category_selectList (
	_limit	integer,
	_offset integer,
	_id_product	integer DEFAULT null,
	_id_category	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF product_product_category
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM product_product_category WHERE
	 	(_id_product is null OR id_product = _id_product)	
	AND (_id_category is null OR id_category = _id_category)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_product_product_category_count (
	_id_product	integer DEFAULT null,
	_id_category	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM product_product_category WHERE
	 	(_id_product is null OR id_product = _id_product)	
	AND (_id_category is null OR id_category = _id_category)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_product_product_category_delete(_id_product integer,	_id_category integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM product_product_category
 		WHERE "id_product" = _id_product AND "id_category" = _id_category;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_product_product_category_update 
(	_id_product integer,
	_id_category integer,
	_note character varying,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE product_product_category 
	SET 		note = _note,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id_product = _id_product and id_category = _id_category;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of product_product_category

-- project
CREATE OR REPLACE FUNCTION sp_project_select(_id	integer)
RETURNS SETOF project
AS $$
BEGIN
	RETURN QUERY SELECT * FROM project WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_project_delete(_id integer)
  RETURNS integer AS
$BODY$
BEGIN
		DELETE FROM project
 		WHERE "id" = _id;
		RETURN 0;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION sp_project_delete(integer) OWNER TO postgres;

CREATE OR REPLACE FUNCTION sp_project_insert(
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_type	integer DEFAULT null,
	_pm	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_is_template	boolean DEFAULT null,
	_status	integer DEFAULT null,
	_event_date	timestamp without time zone DEFAULT null,
	_plan_start_date	timestamp without time zone DEFAULT null,
	_plan_end_date	timestamp without time zone DEFAULT null,
	_real_start_date	timestamp without time zone DEFAULT null,
	_real_end_date	timestamp without time zone DEFAULT null,
	_attendees	integer DEFAULT null,
	_id_venue	integer DEFAULT null,
	_creator	integer DEFAULT null,
	_avatar	character varying(255) DEFAULT null,
	_budget	numeric DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO project(name,code,description,id_type,pm,id_company,is_template,status,event_date,plan_start_date,plan_end_date,real_start_date,real_end_date,attendees,id_venue,creator,updator,deletion_time,avatar,budget)
	VALUES(_name,_code,_description,_id_type,_pm,_id_company,_is_template,_status,_event_date,_plan_start_date,_plan_end_date,_real_start_date,_real_end_date,_attendees,_id_venue,_creator,_creator,null,_avatar,_budget);
RETURN currval('project_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_project_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_type	integer DEFAULT null,
	_pm	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_is_template	boolean DEFAULT null,
	_status	integer DEFAULT null,
	_event_dateFrom	timestamp without time zone DEFAULT null,
	_event_dateTo	timestamp without time zone DEFAULT null,
	_plan_start_dateFrom	timestamp without time zone DEFAULT null,
	_plan_start_dateTo	timestamp without time zone DEFAULT null,
	_plan_end_dateFrom	timestamp without time zone DEFAULT null,
	_plan_end_dateTo	timestamp without time zone DEFAULT null,
	_real_start_dateFrom	timestamp without time zone DEFAULT null,
	_real_start_dateTo	timestamp without time zone DEFAULT null,
	_real_end_dateFrom	timestamp without time zone DEFAULT null,
	_real_end_dateTo	timestamp without time zone DEFAULT null,
	_attendees	integer DEFAULT null,
	_id_venue	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_avatar	character varying(255) DEFAULT null,
	_budget	numeric DEFAULT null
)
RETURNS SETOF project
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM project WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_pm is null OR pm = _pm)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_is_template is null OR is_template = _is_template)	
	AND (_status is null OR status = _status)	
	AND (_event_dateFrom is null OR event_date >= _event_dateFrom)
	AND	(_event_dateTo is null OR event_date <= _event_dateTo)	
	AND (_plan_start_dateFrom is null OR plan_start_date >= _plan_start_dateFrom)
	AND	(_plan_start_dateTo is null OR plan_start_date <= _plan_start_dateTo)	
	AND (_plan_end_dateFrom is null OR plan_end_date >= _plan_end_dateFrom)
	AND	(_plan_end_dateTo is null OR plan_end_date <= _plan_end_dateTo)	
	AND (_real_start_dateFrom is null OR real_start_date >= _real_start_dateFrom)
	AND	(_real_start_dateTo is null OR real_start_date <= _real_start_dateTo)	
	AND (_real_end_dateFrom is null OR real_end_date >= _real_end_dateFrom)
	AND	(_real_end_dateTo is null OR real_end_date <= _real_end_dateTo)	
	AND (_attendees is null OR attendees = _attendees)	
	AND (_id_venue is null OR id_venue = _id_venue)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_avatar is null OR avatar = _avatar)	
	AND (_budget is null OR budget = _budget)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_project_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_type	integer DEFAULT null,
	_pm	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_is_template	boolean DEFAULT null,
	_status	integer DEFAULT null,
	_event_dateFrom	timestamp without time zone DEFAULT null,
	_event_dateTo	timestamp without time zone DEFAULT null,
	_plan_start_dateFrom	timestamp without time zone DEFAULT null,
	_plan_start_dateTo	timestamp without time zone DEFAULT null,
	_plan_end_dateFrom	timestamp without time zone DEFAULT null,
	_plan_end_dateTo	timestamp without time zone DEFAULT null,
	_real_start_dateFrom	timestamp without time zone DEFAULT null,
	_real_start_dateTo	timestamp without time zone DEFAULT null,
	_real_end_dateFrom	timestamp without time zone DEFAULT null,
	_real_end_dateTo	timestamp without time zone DEFAULT null,
	_attendees	integer DEFAULT null,
	_id_venue	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_avatar	character varying(255) DEFAULT null,
	_budget	numeric DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM project WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_pm is null OR pm = _pm)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_is_template is null OR is_template = _is_template)	
	AND (_status is null OR status = _status)	
	AND (_event_dateFrom is null OR event_date >= _event_dateFrom)
	AND	(_event_dateTo is null OR event_date <= _event_dateTo)	
	AND (_plan_start_dateFrom is null OR plan_start_date >= _plan_start_dateFrom)
	AND	(_plan_start_dateTo is null OR plan_start_date <= _plan_start_dateTo)	
	AND (_plan_end_dateFrom is null OR plan_end_date >= _plan_end_dateFrom)
	AND	(_plan_end_dateTo is null OR plan_end_date <= _plan_end_dateTo)	
	AND (_real_start_dateFrom is null OR real_start_date >= _real_start_dateFrom)
	AND	(_real_start_dateTo is null OR real_start_date <= _real_start_dateTo)	
	AND (_real_end_dateFrom is null OR real_end_date >= _real_end_dateFrom)
	AND	(_real_end_dateTo is null OR real_end_date <= _real_end_dateTo)	
	AND (_attendees is null OR attendees = _attendees)	
	AND (_id_venue is null OR id_venue = _id_venue)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_avatar is null OR avatar = _avatar)	
	AND (_budget is null OR budget = _budget);
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION sp_project_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_id_type integer,
	_pm integer,
	_id_company integer,
	_is_template boolean,
	_status integer,
	_event_date timestamp without time zone,
	_plan_start_date timestamp without time zone,
	_plan_end_date timestamp without time zone,
	_real_start_date timestamp without time zone,
	_real_end_date timestamp without time zone,
	_attendees integer,
	_id_venue integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone,
	_avatar character varying(255),
	_budget numeric  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE project 
	SET 		name = _name,
		code = _code,
		description = _description,
		id_type = _id_type,
		pm = _pm,
		id_company = _id_company,
		is_template = _is_template,
		status = _status,
		event_date = _event_date,
		plan_start_date = _plan_start_date,
		plan_end_date = _plan_end_date,
		real_start_date = _real_start_date,
		real_end_date = _real_end_date,
		attendees = _attendees,
		id_venue = _id_venue,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time,
		avatar = _avatar,
		budget = _budget
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;
-- end of project

-- project_competitor
CREATE OR REPLACE FUNCTION sp_project_competitor_insert(
	_id_project	integer,
	_id_company	integer,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO project_competitor(id_project,id_company,note,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_project,_id_company,_note,_status,_is_draft,_notification,_creator,_creator,null);
RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_project_competitor_select(_id_project	integer , _id_company	integer)
RETURNS SETOF project_competitor
AS $$
BEGIN
	RETURN QUERY SELECT * FROM project_competitor WHERE id_project = _id_project AND id_company = _id_company;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_project_competitor_selectList (
	_limit	integer,
	_offset integer,
	_id_project	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF project_competitor
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM project_competitor WHERE
	 	(_id_project is null OR id_project = _id_project)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_project_competitor_count (
	_id_project	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM project_competitor WHERE
	 	(_id_project is null OR id_project = _id_project)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_project_competitor_delete(_id_project integer,	_id_company integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM project_competitor
 		WHERE "id_project" = _id_project AND "id_company" = _id_company;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_project_competitor_update 
(	_id_project integer,
	_id_company integer,
	_note character varying,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE project_competitor 
	SET 		note = _note,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id_project = _id_project and id_company = _id_company;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of project_competitor

-- project_industry
CREATE OR REPLACE FUNCTION sp_project_industry_insert(
	_id_project	integer DEFAULT null,
	_id_industry	integer DEFAULT null,
	_id_brand	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO project_industry(id_project,id_industry,id_brand,note,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_project,_id_industry,_id_brand,_note,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('project_industry_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_project_industry_select(_id	integer)
RETURNS SETOF project_industry
AS $$
BEGIN
	RETURN QUERY SELECT * FROM project_industry WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_project_industry_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_id_industry	integer DEFAULT null,
	_id_brand	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF project_industry
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM project_industry WHERE
	 	(_id is null OR id = _id)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_id_industry is null OR id_industry = _id_industry)	
	AND (_id_brand is null OR id_brand = _id_brand)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_project_industry_count (
	_id	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_id_industry	integer DEFAULT null,
	_id_brand	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM project_industry WHERE
	 	(_id is null OR id = _id)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_id_industry is null OR id_industry = _id_industry)	
	AND (_id_brand is null OR id_brand = _id_brand)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_project_industry_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM project_industry
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_project_industry_update 
(	_id integer,
	_id_project integer,
	_id_industry integer,
	_id_brand integer,
	_note character varying,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE project_industry 
	SET 		id_project = _id_project,
		id_industry = _id_industry,
		id_brand = _id_brand,
		note = _note,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of project_industry

-- project_type
CREATE OR REPLACE FUNCTION sp_project_type_insert(
	_name	character varying(255),
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO project_type(name,code,description,id_parent,is_draft,creator,updator,deletion_time)
	VALUES(_name,_code,_description,_id_parent,_is_draft,_creator,_creator,null);
RETURN currval('project_type_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_project_type_select(_id	integer)
RETURNS SETOF project_type
AS $$
BEGIN
	RETURN QUERY SELECT * FROM project_type WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_project_type_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF project_type
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM project_type WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_project_type_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM project_type WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_project_type_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM project_type
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_project_type_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_id_parent integer,
	_is_draft boolean,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE project_type 
	SET 		name = _name,
		code = _code,
		description = _description,
		id_parent = _id_parent,
		is_draft = _is_draft,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of project_type

-- purchase
CREATE OR REPLACE FUNCTION sp_purchase_select(_id	integer)
RETURNS SETOF purchase
AS $$
BEGIN
	RETURN QUERY SELECT * FROM purchase WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_purchase_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM purchase
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_purchase_insert(
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_plan_cost	numeric DEFAULT null,
	_real_cost	numeric DEFAULT null,
	_plan_order_time	timestamp without time zone DEFAULT null,
	_plan_delivery_time	timestamp without time zone DEFAULT null,
	_real_order_tate	timestamp without time zone DEFAULT null,
	_real_delivery_time	timestamp without time zone DEFAULT null,
	_need_approval	boolean DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null,
	_remark	character varying DEFAULT null,
	_purchaser	integer DEFAULT null,
	_supplier	integer DEFAULT null,
	_id_currency	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_discount	numeric DEFAULT null,
	_additional_cost	numeric DEFAULT null,
	_order_time	timestamp without time zone DEFAULT null,
	_id_quotation	integer DEFAULT null,
	_id_quotation_item	integer DEFAULT null,
	_id_checklist	integer DEFAULT null,
	_id_task	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO purchase(name,code,description,plan_cost,real_cost,plan_order_time,plan_delivery_time,real_order_tate,real_delivery_time,need_approval,status,is_draft,notification,creator,updator,deletion_time,remark,purchaser,supplier,id_currency,id_project,discount,additional_cost,order_time,id_quotation,id_quotation_item,id_checklist,id_task)
	VALUES(_name,_code,_description,_plan_cost,_real_cost,_plan_order_time,_plan_delivery_time,_real_order_tate,_real_delivery_time,_need_approval,_status,_is_draft,_notification,_creator,_creator,null,_remark,_purchaser,_supplier,_id_currency,_id_project,_discount,_additional_cost,_order_time,_id_quotation,_id_quotation_item,_id_checklist,_id_task);
RETURN currval('purchase_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_purchase_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_plan_cost	numeric DEFAULT null,
	_real_cost	numeric DEFAULT null,
	_plan_order_timeFrom	timestamp without time zone DEFAULT null,
	_plan_order_timeTo	timestamp without time zone DEFAULT null,
	_plan_delivery_timeFrom	timestamp without time zone DEFAULT null,
	_plan_delivery_timeTo	timestamp without time zone DEFAULT null,
	_real_order_tateFrom	timestamp without time zone DEFAULT null,
	_real_order_tateTo	timestamp without time zone DEFAULT null,
	_real_delivery_timeFrom	timestamp without time zone DEFAULT null,
	_real_delivery_timeTo	timestamp without time zone DEFAULT null,
	_need_approval	boolean DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_remark	character varying DEFAULT null,
	_purchaser	integer DEFAULT null,
	_supplier	integer DEFAULT null,
	_id_currency	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_discount	numeric DEFAULT null,
	_additional_cost	numeric DEFAULT null,
	_order_timeFrom	timestamp without time zone DEFAULT null,
	_order_timeTo	timestamp without time zone DEFAULT null,
	_id_quotation	integer DEFAULT null,
	_id_quotation_item	integer DEFAULT null,
	_id_checklist	integer DEFAULT null,
	_id_task	integer DEFAULT null
)
RETURNS SETOF purchase
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM purchase WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_plan_cost is null OR plan_cost = _plan_cost)	
	AND (_real_cost is null OR real_cost = _real_cost)	
	AND (_plan_order_timeFrom is null OR plan_order_time >= _plan_order_timeFrom)
	AND	(_plan_order_timeTo is null OR plan_order_time <= _plan_order_timeTo)	
	AND (_plan_delivery_timeFrom is null OR plan_delivery_time >= _plan_delivery_timeFrom)
	AND	(_plan_delivery_timeTo is null OR plan_delivery_time <= _plan_delivery_timeTo)	
	AND (_real_order_tateFrom is null OR real_order_tate >= _real_order_tateFrom)
	AND	(_real_order_tateTo is null OR real_order_tate <= _real_order_tateTo)	
	AND (_real_delivery_timeFrom is null OR real_delivery_time >= _real_delivery_timeFrom)
	AND	(_real_delivery_timeTo is null OR real_delivery_time <= _real_delivery_timeTo)	
	AND (_need_approval is null OR need_approval = _need_approval)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_remark is null OR remark = _remark)	
	AND (_purchaser is null OR purchaser = _purchaser)	
	AND (_supplier is null OR supplier = _supplier)	
	AND (_id_currency is null OR id_currency = _id_currency)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_discount is null OR discount = _discount)	
	AND (_additional_cost is null OR additional_cost = _additional_cost)	
	AND (_order_timeFrom is null OR order_time >= _order_timeFrom)
	AND	(_order_timeTo is null OR order_time <= _order_timeTo)	
	AND (_id_quotation is null OR id_quotation = _id_quotation)	
	AND (_id_quotation_item is null OR id_quotation_item = _id_quotation_item)	
	AND (_id_checklist is null OR id_checklist = _id_checklist)	
	AND (_id_task is null OR id_task = _id_task)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_purchase_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_plan_cost	numeric DEFAULT null,
	_real_cost	numeric DEFAULT null,
	_plan_order_timeFrom	timestamp without time zone DEFAULT null,
	_plan_order_timeTo	timestamp without time zone DEFAULT null,
	_plan_delivery_timeFrom	timestamp without time zone DEFAULT null,
	_plan_delivery_timeTo	timestamp without time zone DEFAULT null,
	_real_order_tateFrom	timestamp without time zone DEFAULT null,
	_real_order_tateTo	timestamp without time zone DEFAULT null,
	_real_delivery_timeFrom	timestamp without time zone DEFAULT null,
	_real_delivery_timeTo	timestamp without time zone DEFAULT null,
	_need_approval	boolean DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_remark	character varying DEFAULT null,
	_purchaser	integer DEFAULT null,
	_supplier	integer DEFAULT null,
	_id_currency	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_discount	numeric DEFAULT null,
	_additional_cost	numeric DEFAULT null,
	_order_timeFrom	timestamp without time zone DEFAULT null,
	_order_timeTo	timestamp without time zone DEFAULT null,
	_id_quotation	integer DEFAULT null,
	_id_quotation_item	integer DEFAULT null,
	_id_checklist	integer DEFAULT null,
	_id_task	integer DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM purchase WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_plan_cost is null OR plan_cost = _plan_cost)	
	AND (_real_cost is null OR real_cost = _real_cost)	
	AND (_plan_order_timeFrom is null OR plan_order_time >= _plan_order_timeFrom)
	AND	(_plan_order_timeTo is null OR plan_order_time <= _plan_order_timeTo)	
	AND (_plan_delivery_timeFrom is null OR plan_delivery_time >= _plan_delivery_timeFrom)
	AND	(_plan_delivery_timeTo is null OR plan_delivery_time <= _plan_delivery_timeTo)	
	AND (_real_order_tateFrom is null OR real_order_tate >= _real_order_tateFrom)
	AND	(_real_order_tateTo is null OR real_order_tate <= _real_order_tateTo)	
	AND (_real_delivery_timeFrom is null OR real_delivery_time >= _real_delivery_timeFrom)
	AND	(_real_delivery_timeTo is null OR real_delivery_time <= _real_delivery_timeTo)	
	AND (_need_approval is null OR need_approval = _need_approval)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_remark is null OR remark = _remark)	
	AND (_purchaser is null OR purchaser = _purchaser)	
	AND (_supplier is null OR supplier = _supplier)	
	AND (_id_currency is null OR id_currency = _id_currency)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_discount is null OR discount = _discount)	
	AND (_additional_cost is null OR additional_cost = _additional_cost)	
	AND (_order_timeFrom is null OR order_time >= _order_timeFrom)
	AND	(_order_timeTo is null OR order_time <= _order_timeTo)	
	AND (_id_quotation is null OR id_quotation = _id_quotation)	
	AND (_id_quotation_item is null OR id_quotation_item = _id_quotation_item)	
	AND (_id_checklist is null OR id_checklist = _id_checklist)	
	AND (_id_task is null OR id_task = _id_task);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_purchase_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_plan_cost numeric,
	_real_cost numeric,
	_plan_order_time timestamp without time zone,
	_plan_delivery_time timestamp without time zone,
	_real_order_tate timestamp without time zone,
	_real_delivery_time timestamp without time zone,
	_need_approval boolean,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone,
	_remark character varying,
	_purchaser integer,
	_supplier integer,
	_id_currency integer,
	_id_project integer,
	_discount numeric,
	_additional_cost numeric,
	_order_time timestamp without time zone,
	_id_quotation integer,
	_id_quotation_item integer,
	_id_checklist integer,
	_id_task integer  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE purchase 
	SET 		name = _name,
		code = _code,
		description = _description,
		plan_cost = _plan_cost,
		real_cost = _real_cost,
		plan_order_time = _plan_order_time,
		plan_delivery_time = _plan_delivery_time,
		real_order_tate = _real_order_tate,
		real_delivery_time = _real_delivery_time,
		need_approval = _need_approval,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time,
		remark = _remark,
		purchaser = _purchaser,
		supplier = _supplier,
		id_currency = _id_currency,
		id_project = _id_project,
		discount = _discount,
		additional_cost = _additional_cost,
		order_time = _order_time,
		id_quotation = _id_quotation,
		id_quotation_item = _id_quotation_item,
		id_checklist = _id_checklist,
		id_task = _id_task
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;
-- end of purchase

-- purchase_item
CREATE OR REPLACE FUNCTION sp_purchase_item_select(_id	integer)
RETURNS SETOF purchase_item
AS $$
BEGIN
	RETURN QUERY SELECT * FROM purchase_item WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_purchase_item_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM purchase_item
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_purchase_item_insert(
	_id_purchase	integer DEFAULT null,
	_id_product	integer DEFAULT null,
	_from_date	timestamp without time zone DEFAULT null,
	_to_date	timestamp without time zone DEFAULT null,
	_quantity	numeric DEFAULT null,
	_id_unit	integer DEFAULT null,
	_unit_price	integer DEFAULT null,
	_total_price	numeric DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null,
	_id_service	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_item_price	numeric DEFAULT null,
	_discount	numeric DEFAULT null,
	_plan_delivery_time	timestamp without time zone DEFAULT null,
	_real_delivery_time	timestamp without time zone DEFAULT null,
	_requester	integer DEFAULT null,
	_description	character varying DEFAULT null,
	_remark	character varying DEFAULT null,
	_id_project	integer DEFAULT null,
	_id_task	integer DEFAULT null,
	_id_quotation	integer DEFAULT null,
	_id_quotation_item	integer DEFAULT null,
	_additional_cost	numeric DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO purchase_item(id_purchase,id_product,from_date,to_date,quantity,id_unit,unit_price,total_price,note,status,is_draft,notification,creator,updator,deletion_time,id_service,name,code,item_price,discount,plan_delivery_time,real_delivery_time,requester,description,remark,id_project,id_task,id_quotation,id_quotation_item,additional_cost)
	VALUES(_id_purchase,_id_product,_from_date,_to_date,_quantity,_id_unit,_unit_price,_total_price,_note,_status,_is_draft,_notification,_creator,_creator,null,_id_service,_name,_code,_item_price,_discount,_plan_delivery_time,_real_delivery_time,_requester,_description,_remark,_id_project,_id_task,_id_quotation,_id_quotation_item,_additional_cost);
RETURN currval('purchase_item_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_purchase_item_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_purchase	integer DEFAULT null,
	_id_product	integer DEFAULT null,
	_from_dateFrom	timestamp without time zone DEFAULT null,
	_from_dateTo	timestamp without time zone DEFAULT null,
	_to_dateFrom	timestamp without time zone DEFAULT null,
	_to_dateTo	timestamp without time zone DEFAULT null,
	_quantity	numeric DEFAULT null,
	_id_unit	integer DEFAULT null,
	_unit_price	integer DEFAULT null,
	_total_price	numeric DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_id_service	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_item_price	numeric DEFAULT null,
	_discount	numeric DEFAULT null,
	_plan_delivery_timeFrom	timestamp without time zone DEFAULT null,
	_plan_delivery_timeTo	timestamp without time zone DEFAULT null,
	_real_delivery_timeFrom	timestamp without time zone DEFAULT null,
	_real_delivery_timeTo	timestamp without time zone DEFAULT null,
	_requester	integer DEFAULT null,
	_description	character varying DEFAULT null,
	_remark	character varying DEFAULT null,
	_id_project	integer DEFAULT null,
	_id_task	integer DEFAULT null,
	_id_quotation	integer DEFAULT null,
	_id_quotation_item	integer DEFAULT null,
	_additional_cost	numeric DEFAULT null
)
RETURNS SETOF purchase_item
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM purchase_item WHERE
	 	(_id is null OR id = _id)	
	AND (_id_purchase is null OR id_purchase = _id_purchase)	
	AND (_id_product is null OR id_product = _id_product)	
	AND (_from_dateFrom is null OR from_date >= _from_dateFrom)
	AND	(_from_dateTo is null OR from_date <= _from_dateTo)	
	AND (_to_dateFrom is null OR to_date >= _to_dateFrom)
	AND	(_to_dateTo is null OR to_date <= _to_dateTo)	
	AND (_quantity is null OR quantity = _quantity)	
	AND (_id_unit is null OR id_unit = _id_unit)	
	AND (_unit_price is null OR unit_price = _unit_price)	
	AND (_total_price is null OR total_price = _total_price)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_id_service is null OR id_service = _id_service)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_item_price is null OR item_price = _item_price)	
	AND (_discount is null OR discount = _discount)	
	AND (_plan_delivery_timeFrom is null OR plan_delivery_time >= _plan_delivery_timeFrom)
	AND	(_plan_delivery_timeTo is null OR plan_delivery_time <= _plan_delivery_timeTo)	
	AND (_real_delivery_timeFrom is null OR real_delivery_time >= _real_delivery_timeFrom)
	AND	(_real_delivery_timeTo is null OR real_delivery_time <= _real_delivery_timeTo)	
	AND (_requester is null OR requester = _requester)	
	AND (_description is null OR description = _description)	
	AND (_remark is null OR remark = _remark)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_id_task is null OR id_task = _id_task)	
	AND (_id_quotation is null OR id_quotation = _id_quotation)	
	AND (_id_quotation_item is null OR id_quotation_item = _id_quotation_item)	
	AND (_additional_cost is null OR additional_cost = _additional_cost)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_purchase_item_count (
	_id	integer DEFAULT null,
	_id_purchase	integer DEFAULT null,
	_id_product	integer DEFAULT null,
	_from_dateFrom	timestamp without time zone DEFAULT null,
	_from_dateTo	timestamp without time zone DEFAULT null,
	_to_dateFrom	timestamp without time zone DEFAULT null,
	_to_dateTo	timestamp without time zone DEFAULT null,
	_quantity	numeric DEFAULT null,
	_id_unit	integer DEFAULT null,
	_unit_price	integer DEFAULT null,
	_total_price	numeric DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_id_service	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_item_price	numeric DEFAULT null,
	_discount	numeric DEFAULT null,
	_plan_delivery_timeFrom	timestamp without time zone DEFAULT null,
	_plan_delivery_timeTo	timestamp without time zone DEFAULT null,
	_real_delivery_timeFrom	timestamp without time zone DEFAULT null,
	_real_delivery_timeTo	timestamp without time zone DEFAULT null,
	_requester	integer DEFAULT null,
	_description	character varying DEFAULT null,
	_remark	character varying DEFAULT null,
	_id_project	integer DEFAULT null,
	_id_task	integer DEFAULT null,
	_id_quotation	integer DEFAULT null,
	_id_quotation_item	integer DEFAULT null,
	_additional_cost	numeric DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM purchase_item WHERE
	 	(_id is null OR id = _id)	
	AND (_id_purchase is null OR id_purchase = _id_purchase)	
	AND (_id_product is null OR id_product = _id_product)	
	AND (_from_dateFrom is null OR from_date >= _from_dateFrom)
	AND	(_from_dateTo is null OR from_date <= _from_dateTo)	
	AND (_to_dateFrom is null OR to_date >= _to_dateFrom)
	AND	(_to_dateTo is null OR to_date <= _to_dateTo)	
	AND (_quantity is null OR quantity = _quantity)	
	AND (_id_unit is null OR id_unit = _id_unit)	
	AND (_unit_price is null OR unit_price = _unit_price)	
	AND (_total_price is null OR total_price = _total_price)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_id_service is null OR id_service = _id_service)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_item_price is null OR item_price = _item_price)	
	AND (_discount is null OR discount = _discount)	
	AND (_plan_delivery_timeFrom is null OR plan_delivery_time >= _plan_delivery_timeFrom)
	AND	(_plan_delivery_timeTo is null OR plan_delivery_time <= _plan_delivery_timeTo)	
	AND (_real_delivery_timeFrom is null OR real_delivery_time >= _real_delivery_timeFrom)
	AND	(_real_delivery_timeTo is null OR real_delivery_time <= _real_delivery_timeTo)	
	AND (_requester is null OR requester = _requester)	
	AND (_description is null OR description = _description)	
	AND (_remark is null OR remark = _remark)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_id_task is null OR id_task = _id_task)	
	AND (_id_quotation is null OR id_quotation = _id_quotation)	
	AND (_id_quotation_item is null OR id_quotation_item = _id_quotation_item)	
	AND (_additional_cost is null OR additional_cost = _additional_cost);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_purchase_item_update 
(	_id integer,
	_id_purchase integer,
	_id_product integer,
	_from_date timestamp without time zone,
	_to_date timestamp without time zone,
	_quantity numeric,
	_id_unit integer,
	_unit_price integer,
	_total_price numeric,
	_note character varying,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone,
	_id_service integer,
	_name character varying(255),
	_code character varying(20),
	_item_price numeric,
	_discount numeric,
	_plan_delivery_time timestamp without time zone,
	_real_delivery_time timestamp without time zone,
	_requester integer,
	_description character varying,
	_remark character varying,
	_id_project integer,
	_id_task integer,
	_id_quotation integer,
	_id_quotation_item integer,
	_additional_cost numeric  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE purchase_item 
	SET 		id_purchase = _id_purchase,
		id_product = _id_product,
		from_date = _from_date,
		to_date = _to_date,
		quantity = _quantity,
		id_unit = _id_unit,
		unit_price = _unit_price,
		total_price = _total_price,
		note = _note,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time,
		id_service = _id_service,
		name = _name,
		code = _code,
		item_price = _item_price,
		discount = _discount,
		plan_delivery_time = _plan_delivery_time,
		real_delivery_time = _real_delivery_time,
		requester = _requester,
		description = _description,
		remark = _remark,
		id_project = _id_project,
		id_task = _id_task,
		id_quotation = _id_quotation,
		id_quotation_item = _id_quotation_item,
		additional_cost = _additional_cost
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;
-- end of purchase_item

-- quotation
CREATE OR REPLACE FUNCTION sp_quotation_insert(
	_id_ref	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_author	integer DEFAULT null,
	_id_approver	integer DEFAULT null,
	_issue_date	timestamp without time zone DEFAULT null,
	_remark	character varying DEFAULT null,
	_id_currency	integer DEFAULT null,
	_total	numeric DEFAULT null,
	_management_fee	numeric DEFAULT null,
	_vat_rate	numeric DEFAULT null,
	_grand_total	numeric DEFAULT null,
	_id_type	integer DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO quotation(id_ref,id_project,name,code,description,id_author,id_approver,issue_date,remark,id_currency,total,management_fee,vat_rate,grand_total,id_type,status,notification,creator,updator,deletion_time)
	VALUES(_id_ref,_id_project,_name,_code,_description,_id_author,_id_approver,_issue_date,_remark,_id_currency,_total,_management_fee,_vat_rate,_grand_total,_id_type,_status,_notification,_creator,_creator,null);
RETURN currval('quotation_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_quotation_select(_id	integer)
RETURNS SETOF quotation
AS $$
BEGIN
	RETURN QUERY SELECT * FROM quotation WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_quotation_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_ref	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_author	integer DEFAULT null,
	_id_approver	integer DEFAULT null,
	_issue_dateFrom	timestamp without time zone DEFAULT null,
	_issue_dateTo	timestamp without time zone DEFAULT null,
	_remark	character varying DEFAULT null,
	_id_currency	integer DEFAULT null,
	_total	numeric DEFAULT null,
	_management_fee	numeric DEFAULT null,
	_vat_rate	numeric DEFAULT null,
	_grand_total	numeric DEFAULT null,
	_id_type	integer DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF quotation
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM quotation WHERE
	 	(_id is null OR id = _id)	
	AND (_id_ref is null OR id_ref = _id_ref)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_author is null OR id_author = _id_author)	
	AND (_id_approver is null OR id_approver = _id_approver)	
	AND (_issue_dateFrom is null OR issue_date >= _issue_dateFrom)
	AND	(_issue_dateTo is null OR issue_date <= _issue_dateTo)	
	AND (_remark is null OR remark = _remark)	
	AND (_id_currency is null OR id_currency = _id_currency)	
	AND (_total is null OR total = _total)	
	AND (_management_fee is null OR management_fee = _management_fee)	
	AND (_vat_rate is null OR vat_rate = _vat_rate)	
	AND (_grand_total is null OR grand_total = _grand_total)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_status is null OR status = _status)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_quotation_count (
	_id	integer DEFAULT null,
	_id_ref	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_author	integer DEFAULT null,
	_id_approver	integer DEFAULT null,
	_issue_dateFrom	timestamp without time zone DEFAULT null,
	_issue_dateTo	timestamp without time zone DEFAULT null,
	_remark	character varying DEFAULT null,
	_id_currency	integer DEFAULT null,
	_total	numeric DEFAULT null,
	_management_fee	numeric DEFAULT null,
	_vat_rate	numeric DEFAULT null,
	_grand_total	numeric DEFAULT null,
	_id_type	integer DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM quotation WHERE
	 	(_id is null OR id = _id)	
	AND (_id_ref is null OR id_ref = _id_ref)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_author is null OR id_author = _id_author)	
	AND (_id_approver is null OR id_approver = _id_approver)	
	AND (_issue_dateFrom is null OR issue_date >= _issue_dateFrom)
	AND	(_issue_dateTo is null OR issue_date <= _issue_dateTo)	
	AND (_remark is null OR remark = _remark)	
	AND (_id_currency is null OR id_currency = _id_currency)	
	AND (_total is null OR total = _total)	
	AND (_management_fee is null OR management_fee = _management_fee)	
	AND (_vat_rate is null OR vat_rate = _vat_rate)	
	AND (_grand_total is null OR grand_total = _grand_total)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_status is null OR status = _status)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_quotation_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM quotation
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_quotation_update 
(	_id integer,
	_id_ref integer,
	_id_project integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_id_author integer,
	_id_approver integer,
	_issue_date timestamp without time zone,
	_remark character varying,
	_id_currency integer,
	_total numeric,
	_management_fee numeric,
	_vat_rate numeric,
	_grand_total numeric,
	_id_type integer,
	_status integer,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE quotation 
	SET 		id_ref = _id_ref,
		id_project = _id_project,
		name = _name,
		code = _code,
		description = _description,
		id_author = _id_author,
		id_approver = _id_approver,
		issue_date = _issue_date,
		remark = _remark,
		id_currency = _id_currency,
		total = _total,
		management_fee = _management_fee,
		vat_rate = _vat_rate,
		grand_total = _grand_total,
		id_type = _id_type,
		status = _status,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of quotation

-- quotation_item
CREATE OR REPLACE FUNCTION sp_quotation_item_insert(
	_id_quotation	integer,
	_id_parent	integer DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_description	character varying DEFAULT null,
	_remark	character varying DEFAULT null,
	_is_group	boolean DEFAULT null,
	_index	integer DEFAULT null,
	_display_index	character varying(10) DEFAULT null,
	_id_product	integer DEFAULT null,
	_id_service	integer DEFAULT null,
	_id_unit	integer DEFAULT null,
	_id_currency	integer DEFAULT null,
	_estimate_quantity	integer DEFAULT null,
	_quotation_quantity	integer DEFAULT null,
	_internal_quantity	integer DEFAULT null,
	_estimate_unit_cost	numeric DEFAULT null,
	_quotation_unit_cost	numeric DEFAULT null,
	_internal_unit_cost	numeric DEFAULT null,
	_estimate_total_amount	numeric DEFAULT null,
	_quotation_total_amount	numeric DEFAULT null,
	_internal_total_amount	numeric DEFAULT null,
	_in_estimate	boolean DEFAULT null,
	_in_quotation	boolean DEFAULT null,
	_in_internal	boolean DEFAULT null,
	_id_type	integer DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO quotation_item(id_quotation,id_parent,code,name,description,remark,is_group,index,display_index,id_product,id_service,id_unit,id_currency,estimate_quantity,quotation_quantity,internal_quantity,estimate_unit_cost,quotation_unit_cost,internal_unit_cost,estimate_total_amount,quotation_total_amount,internal_total_amount,in_estimate,in_quotation,in_internal,id_type,status,notification,creator,updator,deletion_time)
	VALUES(_id_quotation,_id_parent,_code,_name,_description,_remark,_is_group,_index,_display_index,_id_product,_id_service,_id_unit,_id_currency,_estimate_quantity,_quotation_quantity,_internal_quantity,_estimate_unit_cost,_quotation_unit_cost,_internal_unit_cost,_estimate_total_amount,_quotation_total_amount,_internal_total_amount,_in_estimate,_in_quotation,_in_internal,_id_type,_status,_notification,_creator,_creator,null);
RETURN currval('quotation_item_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_quotation_item_select(_id	integer)
RETURNS SETOF quotation_item
AS $$
BEGIN
	RETURN QUERY SELECT * FROM quotation_item WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_quotation_item_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_quotation	integer DEFAULT null,
	_id_parent	integer DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_description	character varying DEFAULT null,
	_remark	character varying DEFAULT null,
	_is_group	boolean DEFAULT null,
	_index	integer DEFAULT null,
	_display_index	character varying(10) DEFAULT null,
	_id_product	integer DEFAULT null,
	_id_service	integer DEFAULT null,
	_id_unit	integer DEFAULT null,
	_id_currency	integer DEFAULT null,
	_estimate_quantity	integer DEFAULT null,
	_quotation_quantity	integer DEFAULT null,
	_internal_quantity	integer DEFAULT null,
	_estimate_unit_cost	numeric DEFAULT null,
	_quotation_unit_cost	numeric DEFAULT null,
	_internal_unit_cost	numeric DEFAULT null,
	_estimate_total_amount	numeric DEFAULT null,
	_quotation_total_amount	numeric DEFAULT null,
	_internal_total_amount	numeric DEFAULT null,
	_in_estimate	boolean DEFAULT null,
	_in_quotation	boolean DEFAULT null,
	_in_internal	boolean DEFAULT null,
	_id_type	integer DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF quotation_item
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM quotation_item WHERE
	 	(_id is null OR id = _id)	
	AND (_id_quotation is null OR id_quotation = _id_quotation)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_code is null OR code = _code)	
	AND (_name is null OR name = _name)	
	AND (_description is null OR description = _description)	
	AND (_remark is null OR remark = _remark)	
	AND (_is_group is null OR is_group = _is_group)	
	AND (_index is null OR index = _index)	
	AND (_display_index is null OR display_index = _display_index)	
	AND (_id_product is null OR id_product = _id_product)	
	AND (_id_service is null OR id_service = _id_service)	
	AND (_id_unit is null OR id_unit = _id_unit)	
	AND (_id_currency is null OR id_currency = _id_currency)	
	AND (_estimate_quantity is null OR estimate_quantity = _estimate_quantity)	
	AND (_quotation_quantity is null OR quotation_quantity = _quotation_quantity)	
	AND (_internal_quantity is null OR internal_quantity = _internal_quantity)	
	AND (_estimate_unit_cost is null OR estimate_unit_cost = _estimate_unit_cost)	
	AND (_quotation_unit_cost is null OR quotation_unit_cost = _quotation_unit_cost)	
	AND (_internal_unit_cost is null OR internal_unit_cost = _internal_unit_cost)	
	AND (_estimate_total_amount is null OR estimate_total_amount = _estimate_total_amount)	
	AND (_quotation_total_amount is null OR quotation_total_amount = _quotation_total_amount)	
	AND (_internal_total_amount is null OR internal_total_amount = _internal_total_amount)	
	AND (_in_estimate is null OR in_estimate = _in_estimate)	
	AND (_in_quotation is null OR in_quotation = _in_quotation)	
	AND (_in_internal is null OR in_internal = _in_internal)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_status is null OR status = _status)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_quotation_item_count (
	_id	integer DEFAULT null,
	_id_quotation	integer DEFAULT null,
	_id_parent	integer DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_description	character varying DEFAULT null,
	_remark	character varying DEFAULT null,
	_is_group	boolean DEFAULT null,
	_index	integer DEFAULT null,
	_display_index	character varying(10) DEFAULT null,
	_id_product	integer DEFAULT null,
	_id_service	integer DEFAULT null,
	_id_unit	integer DEFAULT null,
	_id_currency	integer DEFAULT null,
	_estimate_quantity	integer DEFAULT null,
	_quotation_quantity	integer DEFAULT null,
	_internal_quantity	integer DEFAULT null,
	_estimate_unit_cost	numeric DEFAULT null,
	_quotation_unit_cost	numeric DEFAULT null,
	_internal_unit_cost	numeric DEFAULT null,
	_estimate_total_amount	numeric DEFAULT null,
	_quotation_total_amount	numeric DEFAULT null,
	_internal_total_amount	numeric DEFAULT null,
	_in_estimate	boolean DEFAULT null,
	_in_quotation	boolean DEFAULT null,
	_in_internal	boolean DEFAULT null,
	_id_type	integer DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM quotation_item WHERE
	 	(_id is null OR id = _id)	
	AND (_id_quotation is null OR id_quotation = _id_quotation)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_code is null OR code = _code)	
	AND (_name is null OR name = _name)	
	AND (_description is null OR description = _description)	
	AND (_remark is null OR remark = _remark)	
	AND (_is_group is null OR is_group = _is_group)	
	AND (_index is null OR index = _index)	
	AND (_display_index is null OR display_index = _display_index)	
	AND (_id_product is null OR id_product = _id_product)	
	AND (_id_service is null OR id_service = _id_service)	
	AND (_id_unit is null OR id_unit = _id_unit)	
	AND (_id_currency is null OR id_currency = _id_currency)	
	AND (_estimate_quantity is null OR estimate_quantity = _estimate_quantity)	
	AND (_quotation_quantity is null OR quotation_quantity = _quotation_quantity)	
	AND (_internal_quantity is null OR internal_quantity = _internal_quantity)	
	AND (_estimate_unit_cost is null OR estimate_unit_cost = _estimate_unit_cost)	
	AND (_quotation_unit_cost is null OR quotation_unit_cost = _quotation_unit_cost)	
	AND (_internal_unit_cost is null OR internal_unit_cost = _internal_unit_cost)	
	AND (_estimate_total_amount is null OR estimate_total_amount = _estimate_total_amount)	
	AND (_quotation_total_amount is null OR quotation_total_amount = _quotation_total_amount)	
	AND (_internal_total_amount is null OR internal_total_amount = _internal_total_amount)	
	AND (_in_estimate is null OR in_estimate = _in_estimate)	
	AND (_in_quotation is null OR in_quotation = _in_quotation)	
	AND (_in_internal is null OR in_internal = _in_internal)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_status is null OR status = _status)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_quotation_item_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM quotation_item
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_quotation_item_update 
(	_id integer,
	_id_quotation integer,
	_id_parent integer,
	_code character varying(20),
	_name character varying(255),
	_description character varying,
	_remark character varying,
	_is_group boolean,
	_index integer,
	_display_index character varying(10),
	_id_product integer,
	_id_service integer,
	_id_unit integer,
	_id_currency integer,
	_estimate_quantity integer,
	_quotation_quantity integer,
	_internal_quantity integer,
	_estimate_unit_cost numeric,
	_quotation_unit_cost numeric,
	_internal_unit_cost numeric,
	_estimate_total_amount numeric,
	_quotation_total_amount numeric,
	_internal_total_amount numeric,
	_in_estimate boolean,
	_in_quotation boolean,
	_in_internal boolean,
	_id_type integer,
	_status integer,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE quotation_item 
	SET 		id_quotation = _id_quotation,
		id_parent = _id_parent,
		code = _code,
		name = _name,
		description = _description,
		remark = _remark,
		is_group = _is_group,
		index = _index,
		display_index = _display_index,
		id_product = _id_product,
		id_service = _id_service,
		id_unit = _id_unit,
		id_currency = _id_currency,
		estimate_quantity = _estimate_quantity,
		quotation_quantity = _quotation_quantity,
		internal_quantity = _internal_quantity,
		estimate_unit_cost = _estimate_unit_cost,
		quotation_unit_cost = _quotation_unit_cost,
		internal_unit_cost = _internal_unit_cost,
		estimate_total_amount = _estimate_total_amount,
		quotation_total_amount = _quotation_total_amount,
		internal_total_amount = _internal_total_amount,
		in_estimate = _in_estimate,
		in_quotation = _in_quotation,
		in_internal = _in_internal,
		id_type = _id_type,
		status = _status,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of quotation_item

-- rate
CREATE OR REPLACE FUNCTION sp_rate_insert(
	_object_type	character varying(50) DEFAULT null,
	_object_id	character varying(50) DEFAULT null,
	_rate	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_id_author	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO rate(object_type,object_id,rate,note,id_author,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_object_type,_object_id,_rate,_note,_id_author,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('rate_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_rate_select(_id	integer)
RETURNS SETOF rate
AS $$
BEGIN
	RETURN QUERY SELECT * FROM rate WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_rate_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	character varying(50) DEFAULT null,
	_rate	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_id_author	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF rate
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM rate WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_rate is null OR rate = _rate)	
	AND (_note is null OR note = _note)	
	AND (_id_author is null OR id_author = _id_author)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_rate_count (
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	character varying(50) DEFAULT null,
	_rate	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_id_author	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM rate WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_rate is null OR rate = _rate)	
	AND (_note is null OR note = _note)	
	AND (_id_author is null OR id_author = _id_author)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_rate_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM rate
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_rate_update 
(	_id integer,
	_object_type character varying(50),
	_object_id character varying(50),
	_rate integer,
	_note character varying,
	_id_author integer,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE rate 
	SET 		object_type = _object_type,
		object_id = _object_id,
		rate = _rate,
		note = _note,
		id_author = _id_author,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of rate

-- rate_type
CREATE OR REPLACE FUNCTION sp_rate_type_insert(
	_object_type	character varying(50) DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_min	integer DEFAULT null,
	_max	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO rate_type(object_type,name,code,description,min,max,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_object_type,_name,_code,_description,_min,_max,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('rate_type_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_rate_type_select(_id	integer)
RETURNS SETOF rate_type
AS $$
BEGIN
	RETURN QUERY SELECT * FROM rate_type WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_rate_type_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_min	integer DEFAULT null,
	_max	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF rate_type
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM rate_type WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_min is null OR min = _min)	
	AND (_max is null OR max = _max)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_rate_type_count (
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_min	integer DEFAULT null,
	_max	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM rate_type WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_min is null OR min = _min)	
	AND (_max is null OR max = _max)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_rate_type_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM rate_type
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_rate_type_update 
(	_id integer,
	_object_type character varying(50),
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_min integer,
	_max integer,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE rate_type 
	SET 		object_type = _object_type,
		name = _name,
		code = _code,
		description = _description,
		min = _min,
		max = _max,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of rate_type

-- receivable
CREATE OR REPLACE FUNCTION sp_receivable_select(_id	integer)
RETURNS SETOF receivable
AS $$
BEGIN
	RETURN QUERY SELECT * FROM receivable WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_receivable_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM receivable
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_receivable_insert(
	_id_company	integer DEFAULT null,
	_id_phase	integer DEFAULT null,
	_plan_date	timestamp without time zone DEFAULT null,
	_real_date	timestamp without time zone DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_description	character varying(511) DEFAULT null,
	_remark	character varying(511) DEFAULT null,
	_receivable_type	integer DEFAULT null,
	_money	numeric DEFAULT null,
	_currency	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO receivable(id_company,id_phase,plan_date,real_date,name,code,status,notification,creator,updator,deletion_time,id_project,description,remark,receivable_type,money,currency)
	VALUES(_id_company,_id_phase,_plan_date,_real_date,_name,_code,_status,_notification,_creator,_creator,null,_id_project,_description,_remark,_receivable_type,_money,_currency);
RETURN currval('receivable_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_receivable_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_id_phase	integer DEFAULT null,
	_plan_dateFrom	timestamp without time zone DEFAULT null,
	_plan_dateTo	timestamp without time zone DEFAULT null,
	_real_dateFrom	timestamp without time zone DEFAULT null,
	_real_dateTo	timestamp without time zone DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_id_project	integer DEFAULT null,
	_description	character varying(511) DEFAULT null,
	_remark	character varying(511) DEFAULT null,
	_receivable_type	integer DEFAULT null,
	_money	numeric DEFAULT null,
	_currency	integer DEFAULT null
)
RETURNS SETOF receivable
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM receivable WHERE
	 	(_id is null OR id = _id)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_id_phase is null OR id_phase = _id_phase)	
	AND (_plan_dateFrom is null OR plan_date >= _plan_dateFrom)
	AND	(_plan_dateTo is null OR plan_date <= _plan_dateTo)	
	AND (_real_dateFrom is null OR real_date >= _real_dateFrom)
	AND	(_real_dateTo is null OR real_date <= _real_dateTo)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_status is null OR status = _status)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_description is null OR description = _description)	
	AND (_remark is null OR remark = _remark)	
	AND (_receivable_type is null OR receivable_type = _receivable_type)	
	AND (_money is null OR money = _money)	
	AND (_currency is null OR currency = _currency)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_receivable_count (
	_id	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_id_phase	integer DEFAULT null,
	_plan_dateFrom	timestamp without time zone DEFAULT null,
	_plan_dateTo	timestamp without time zone DEFAULT null,
	_real_dateFrom	timestamp without time zone DEFAULT null,
	_real_dateTo	timestamp without time zone DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_id_project	integer DEFAULT null,
	_description	character varying(511) DEFAULT null,
	_remark	character varying(511) DEFAULT null,
	_receivable_type	integer DEFAULT null,
	_money	numeric DEFAULT null,
	_currency	integer DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM receivable WHERE
	 	(_id is null OR id = _id)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_id_phase is null OR id_phase = _id_phase)	
	AND (_plan_dateFrom is null OR plan_date >= _plan_dateFrom)
	AND	(_plan_dateTo is null OR plan_date <= _plan_dateTo)	
	AND (_real_dateFrom is null OR real_date >= _real_dateFrom)
	AND	(_real_dateTo is null OR real_date <= _real_dateTo)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_status is null OR status = _status)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_description is null OR description = _description)	
	AND (_remark is null OR remark = _remark)	
	AND (_receivable_type is null OR receivable_type = _receivable_type)	
	AND (_money is null OR money = _money)	
	AND (_currency is null OR currency = _currency);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_receivable_update 
(	_id integer,
	_id_company integer,
	_id_phase integer,
	_plan_date timestamp without time zone,
	_real_date timestamp without time zone,
	_name character varying(255),
	_code character varying(20),
	_status integer,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone,
	_id_project integer,
	_description character varying(511),
	_remark character varying(511),
	_receivable_type integer,
	_money numeric,
	_currency integer  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE receivable 
	SET 		id_company = _id_company,
		id_phase = _id_phase,
		plan_date = _plan_date,
		real_date = _real_date,
		name = _name,
		code = _code,
		status = _status,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time,
		id_project = _id_project,
		description = _description,
		remark = _remark,
		receivable_type = _receivable_type,
		money = _money,
		currency = _currency
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of receivable

-- relation_type
CREATE OR REPLACE FUNCTION sp_relation_type_insert(
	_name	character varying(255),
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO relation_type(name,code,description,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_name,_code,_description,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('relation_type_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_relation_type_select(_id	integer)
RETURNS SETOF relation_type
AS $$
BEGIN
	RETURN QUERY SELECT * FROM relation_type WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_relation_type_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF relation_type
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM relation_type WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_relation_type_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM relation_type WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_relation_type_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM relation_type
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_relation_type_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE relation_type 
	SET 		name = _name,
		code = _code,
		description = _description,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of relation_type

-- relationship
CREATE OR REPLACE FUNCTION sp_relationship_insert(
	_id_source_company	integer DEFAULT null,
	_id_target_company	integer DEFAULT null,
	_id_source_person	integer DEFAULT null,
	_id_target_person	integer DEFAULT null,
	_id_type	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_from_date	timestamp without time zone DEFAULT null,
	_to_date	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO relationship(id_source_company,id_target_company,id_source_person,id_target_person,id_type,note,from_date,to_date,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_source_company,_id_target_company,_id_source_person,_id_target_person,_id_type,_note,_from_date,_to_date,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('relationship_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_relationship_select(_id	integer)
RETURNS SETOF relationship
AS $$
BEGIN
	RETURN QUERY SELECT * FROM relationship WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_relationship_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_source_company	integer DEFAULT null,
	_id_target_company	integer DEFAULT null,
	_id_source_person	integer DEFAULT null,
	_id_target_person	integer DEFAULT null,
	_id_type	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_from_dateFrom	timestamp without time zone DEFAULT null,
	_from_dateTo	timestamp without time zone DEFAULT null,
	_to_dateFrom	timestamp without time zone DEFAULT null,
	_to_dateTo	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF relationship
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM relationship WHERE
	 	(_id is null OR id = _id)	
	AND (_id_source_company is null OR id_source_company = _id_source_company)	
	AND (_id_target_company is null OR id_target_company = _id_target_company)	
	AND (_id_source_person is null OR id_source_person = _id_source_person)	
	AND (_id_target_person is null OR id_target_person = _id_target_person)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_note is null OR note = _note)	
	AND (_from_dateFrom is null OR from_date >= _from_dateFrom)
	AND	(_from_dateTo is null OR from_date <= _from_dateTo)	
	AND (_to_dateFrom is null OR to_date >= _to_dateFrom)
	AND	(_to_dateTo is null OR to_date <= _to_dateTo)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_relationship_count (
	_id	integer DEFAULT null,
	_id_source_company	integer DEFAULT null,
	_id_target_company	integer DEFAULT null,
	_id_source_person	integer DEFAULT null,
	_id_target_person	integer DEFAULT null,
	_id_type	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_from_dateFrom	timestamp without time zone DEFAULT null,
	_from_dateTo	timestamp without time zone DEFAULT null,
	_to_dateFrom	timestamp without time zone DEFAULT null,
	_to_dateTo	timestamp without time zone DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM relationship WHERE
	 	(_id is null OR id = _id)	
	AND (_id_source_company is null OR id_source_company = _id_source_company)	
	AND (_id_target_company is null OR id_target_company = _id_target_company)	
	AND (_id_source_person is null OR id_source_person = _id_source_person)	
	AND (_id_target_person is null OR id_target_person = _id_target_person)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_note is null OR note = _note)	
	AND (_from_dateFrom is null OR from_date >= _from_dateFrom)
	AND	(_from_dateTo is null OR from_date <= _from_dateTo)	
	AND (_to_dateFrom is null OR to_date >= _to_dateFrom)
	AND	(_to_dateTo is null OR to_date <= _to_dateTo)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_relationship_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM relationship
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_relationship_update 
(	_id integer,
	_id_source_company integer,
	_id_target_company integer,
	_id_source_person integer,
	_id_target_person integer,
	_id_type integer,
	_note character varying,
	_from_date timestamp without time zone,
	_to_date timestamp without time zone,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE relationship 
	SET 		id_source_company = _id_source_company,
		id_target_company = _id_target_company,
		id_source_person = _id_source_person,
		id_target_person = _id_target_person,
		id_type = _id_type,
		note = _note,
		from_date = _from_date,
		to_date = _to_date,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of relationship

-- research
CREATE OR REPLACE FUNCTION sp_research_insert(
	_object_type	character varying(50) DEFAULT null,
	_object_id	character varying(50) DEFAULT null,
	_title	character varying(255) DEFAULT null,
	_description	character varying(511) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO research(object_type,object_id,title,description,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_object_type,_object_id,_title,_description,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('research_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_research_select(_id	integer)
RETURNS SETOF research
AS $$
BEGIN
	RETURN QUERY SELECT * FROM research WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_research_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	character varying(50) DEFAULT null,
	_title	character varying(255) DEFAULT null,
	_description	character varying(511) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF research
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM research WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_title is null OR title = _title)	
	AND (_description is null OR description = _description)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_research_count (
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	character varying(50) DEFAULT null,
	_title	character varying(255) DEFAULT null,
	_description	character varying(511) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM research WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_title is null OR title = _title)	
	AND (_description is null OR description = _description)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_research_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM research
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_research_update 
(	_id integer,
	_object_type character varying(50),
	_object_id character varying(50),
	_title character varying(255),
	_description character varying(511),
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE research 
	SET 		object_type = _object_type,
		object_id = _object_id,
		title = _title,
		description = _description,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of research

-- risk
CREATE OR REPLACE FUNCTION sp_risk_insert(
	_object_type	character varying(50) DEFAULT null,
	_object_id	character varying(50) DEFAULT null,
	_id_type	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_level	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO risk(object_type,object_id,id_type,note,level,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_object_type,_object_id,_id_type,_note,_level,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('risk_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_risk_select(_id	integer)
RETURNS SETOF risk
AS $$
BEGIN
	RETURN QUERY SELECT * FROM risk WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_risk_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	character varying(50) DEFAULT null,
	_id_type	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_level	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF risk
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM risk WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_note is null OR note = _note)	
	AND (_level is null OR level = _level)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_risk_count (
	_id	integer DEFAULT null,
	_object_type	character varying(50) DEFAULT null,
	_object_id	character varying(50) DEFAULT null,
	_id_type	integer DEFAULT null,
	_note	character varying DEFAULT null,
	_level	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM risk WHERE
	 	(_id is null OR id = _id)	
	AND (_object_type is null OR object_type = _object_type)	
	AND (_object_id is null OR object_id = _object_id)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_note is null OR note = _note)	
	AND (_level is null OR level = _level)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_risk_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM risk
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_risk_update 
(	_id integer,
	_object_type character varying(50),
	_object_id character varying(50),
	_id_type integer,
	_note character varying,
	_level integer,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE risk 
	SET 		object_type = _object_type,
		object_id = _object_id,
		id_type = _id_type,
		note = _note,
		level = _level,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of risk

-- risk_type
CREATE OR REPLACE FUNCTION sp_risk_type_insert(
	_name	character varying(255),
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO risk_type(name,code,description,id_parent,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_name,_code,_description,_id_parent,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('risk_type_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_risk_type_select(_id	integer)
RETURNS SETOF risk_type
AS $$
BEGIN
	RETURN QUERY SELECT * FROM risk_type WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_risk_type_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF risk_type
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM risk_type WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_risk_type_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM risk_type WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_risk_type_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM risk_type
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_risk_type_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_id_parent integer,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE risk_type 
	SET 		name = _name,
		code = _code,
		description = _description,
		id_parent = _id_parent,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of risk_type

-- role
CREATE OR REPLACE FUNCTION sp_role_insert(
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO role(name,code,description,creator,updator,deletion_time)
	VALUES(_name,_code,_description,_creator,_creator,null);
RETURN currval('role_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_role_select(_id	integer)
RETURNS SETOF role
AS $$
BEGIN
	RETURN QUERY SELECT * FROM role WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_role_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF role
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM role WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_role_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM role WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_role_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM role
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_role_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE role 
	SET 		name = _name,
		code = _code,
		description = _description,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of role

-- role_right
CREATE OR REPLACE FUNCTION sp_role_right_insert(
	_id_role	integer,
	_right_code	integer,
	_scope	integer DEFAULT null,
	_quota	numeric DEFAULT null,
	_note	character varying(511) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO role_right(id_role,right_code,scope,quota,note,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_role,_right_code,_scope,_quota,_note,_status,_is_draft,_notification,_creator,_creator,null);
RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_role_right_select(_id_role	integer , _right_code	integer)
RETURNS SETOF role_right
AS $$
BEGIN
	RETURN QUERY SELECT * FROM role_right WHERE id_role = _id_role AND right_code = _right_code;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_role_right_selectList (
	_limit	integer,
	_offset integer,
	_id_role	integer DEFAULT null,
	_right_code	integer DEFAULT null,
	_scope	integer DEFAULT null,
	_quota	numeric DEFAULT null,
	_note	character varying(511) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF role_right
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM role_right WHERE
	 	(_id_role is null OR id_role = _id_role)	
	AND (_right_code is null OR right_code = _right_code)	
	AND (_scope is null OR scope = _scope)	
	AND (_quota is null OR quota = _quota)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_role_right_count (
	_id_role	integer DEFAULT null,
	_right_code	integer DEFAULT null,
	_scope	integer DEFAULT null,
	_quota	numeric DEFAULT null,
	_note	character varying(511) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM role_right WHERE
	 	(_id_role is null OR id_role = _id_role)	
	AND (_right_code is null OR right_code = _right_code)	
	AND (_scope is null OR scope = _scope)	
	AND (_quota is null OR quota = _quota)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_role_right_delete(_id_role integer,	_right_code integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM role_right
 		WHERE "id_role" = _id_role AND "right_code" = _right_code;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_role_right_update 
(	_id_role integer,
	_right_code integer,
	_scope integer,
	_quota numeric,
	_note character varying(511),
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE role_right 
	SET 		scope = _scope,
		quota = _quota,
		note = _note,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id_role = _id_role and right_code = _right_code;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of role_right

-- service
CREATE OR REPLACE FUNCTION sp_service_select(_id	integer)
RETURNS SETOF service
AS $$
BEGIN
	RETURN QUERY SELECT * FROM service WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_service_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM service
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_service_insert(
	_name	character varying(255) DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null,
	_short_name	character varying(25) DEFAULT null,
	_full_name	character varying(255) DEFAULT null,
	_foreign_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_default_unit	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO service(name,status,notification,creator,updator,deletion_time,short_name,full_name,foreign_name,code,description,id_default_unit)
	VALUES(_name,_status,_notification,_creator,_creator,null,_short_name,_full_name,_foreign_name,_code,_description,_id_default_unit);
RETURN currval('service_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_service_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_short_name	character varying(25) DEFAULT null,
	_full_name	character varying(255) DEFAULT null,
	_foreign_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_default_unit	integer DEFAULT null
)
RETURNS SETOF service
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM service WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_status is null OR status = _status)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_short_name is null OR short_name = _short_name)	
	AND (_full_name is null OR full_name = _full_name)	
	AND (_foreign_name is null OR foreign_name = _foreign_name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_default_unit is null OR id_default_unit = _id_default_unit)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_service_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_status	integer DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null,
	_short_name	character varying(25) DEFAULT null,
	_full_name	character varying(255) DEFAULT null,
	_foreign_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_default_unit	integer DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM service WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_status is null OR status = _status)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	
	AND (_short_name is null OR short_name = _short_name)	
	AND (_full_name is null OR full_name = _full_name)	
	AND (_foreign_name is null OR foreign_name = _foreign_name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_default_unit is null OR id_default_unit = _id_default_unit);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_service_update 
(	_id integer,
	_name character varying(255),
	_status integer,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone,
	_short_name character varying(25),
	_full_name character varying(255),
	_foreign_name character varying(255),
	_code character varying(20),
	_description character varying,
	_id_default_unit integer  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE service 
	SET 		name = _name,
		status = _status,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time,
		short_name = _short_name,
		full_name = _full_name,
		foreign_name = _foreign_name,
		code = _code,
		description = _description,
		id_default_unit = _id_default_unit
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of service

-- supply
CREATE OR REPLACE FUNCTION sp_supply_insert(
	_id_company	integer DEFAULT null,
	_id_product	integer DEFAULT null,
	_from_date	timestamp without time zone DEFAULT null,
	_to_date	timestamp without time zone DEFAULT null,
	_id_unit	integer DEFAULT null,
	_price	numeric DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO supply(id_company,id_product,from_date,to_date,id_unit,price,note,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_company,_id_product,_from_date,_to_date,_id_unit,_price,_note,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('supply_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_supply_select(_id	integer)
RETURNS SETOF supply
AS $$
BEGIN
	RETURN QUERY SELECT * FROM supply WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_supply_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_id_product	integer DEFAULT null,
	_from_dateFrom	timestamp without time zone DEFAULT null,
	_from_dateTo	timestamp without time zone DEFAULT null,
	_to_dateFrom	timestamp without time zone DEFAULT null,
	_to_dateTo	timestamp without time zone DEFAULT null,
	_id_unit	integer DEFAULT null,
	_price	numeric DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF supply
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM supply WHERE
	 	(_id is null OR id = _id)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_id_product is null OR id_product = _id_product)	
	AND (_from_dateFrom is null OR from_date >= _from_dateFrom)
	AND	(_from_dateTo is null OR from_date <= _from_dateTo)	
	AND (_to_dateFrom is null OR to_date >= _to_dateFrom)
	AND	(_to_dateTo is null OR to_date <= _to_dateTo)	
	AND (_id_unit is null OR id_unit = _id_unit)	
	AND (_price is null OR price = _price)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_supply_count (
	_id	integer DEFAULT null,
	_id_company	integer DEFAULT null,
	_id_product	integer DEFAULT null,
	_from_dateFrom	timestamp without time zone DEFAULT null,
	_from_dateTo	timestamp without time zone DEFAULT null,
	_to_dateFrom	timestamp without time zone DEFAULT null,
	_to_dateTo	timestamp without time zone DEFAULT null,
	_id_unit	integer DEFAULT null,
	_price	numeric DEFAULT null,
	_note	character varying DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM supply WHERE
	 	(_id is null OR id = _id)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_id_product is null OR id_product = _id_product)	
	AND (_from_dateFrom is null OR from_date >= _from_dateFrom)
	AND	(_from_dateTo is null OR from_date <= _from_dateTo)	
	AND (_to_dateFrom is null OR to_date >= _to_dateFrom)
	AND	(_to_dateTo is null OR to_date <= _to_dateTo)	
	AND (_id_unit is null OR id_unit = _id_unit)	
	AND (_price is null OR price = _price)	
	AND (_note is null OR note = _note)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_supply_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM supply
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_supply_update 
(	_id integer,
	_id_company integer,
	_id_product integer,
	_from_date timestamp without time zone,
	_to_date timestamp without time zone,
	_id_unit integer,
	_price numeric,
	_note character varying,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE supply 
	SET 		id_company = _id_company,
		id_product = _id_product,
		from_date = _from_date,
		to_date = _to_date,
		id_unit = _id_unit,
		price = _price,
		note = _note,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of supply

-- task
CREATE OR REPLACE FUNCTION sp_task_insert(
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_is_group	boolean DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_type	integer DEFAULT null,
	_id_phase	integer DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_plan_start_time	timestamp without time zone DEFAULT null,
	_plan_end_time	timestamp without time zone DEFAULT null,
	_real_start_tate	timestamp without time zone DEFAULT null,
	_real_end_time	timestamp without time zone DEFAULT null,
	_need_approval	boolean DEFAULT null,
	_is_event	boolean DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_rate	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO task(name,code,description,is_group,id_employee,id_type,id_phase,id_parent,status,plan_start_time,plan_end_time,real_start_tate,real_end_time,need_approval,is_event,is_draft,notification,rate,creator,updator,deletion_time)
	VALUES(_name,_code,_description,_is_group,_id_employee,_id_type,_id_phase,_id_parent,_status,_plan_start_time,_plan_end_time,_real_start_tate,_real_end_time,_need_approval,_is_event,_is_draft,_notification,_rate,_creator,_creator,null);
RETURN currval('task_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_task_select(_id	integer)
RETURNS SETOF task
AS $$
BEGIN
	RETURN QUERY SELECT * FROM task WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_task_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_is_group	boolean DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_type	integer DEFAULT null,
	_id_phase	integer DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_plan_start_timeFrom	timestamp without time zone DEFAULT null,
	_plan_start_timeTo	timestamp without time zone DEFAULT null,
	_plan_end_timeFrom	timestamp without time zone DEFAULT null,
	_plan_end_timeTo	timestamp without time zone DEFAULT null,
	_real_start_tateFrom	timestamp without time zone DEFAULT null,
	_real_start_tateTo	timestamp without time zone DEFAULT null,
	_real_end_timeFrom	timestamp without time zone DEFAULT null,
	_real_end_timeTo	timestamp without time zone DEFAULT null,
	_need_approval	boolean DEFAULT null,
	_is_event	boolean DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_rate	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF task
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM task WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_is_group is null OR is_group = _is_group)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_id_phase is null OR id_phase = _id_phase)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_status is null OR status = _status)	
	AND (_plan_start_timeFrom is null OR plan_start_time >= _plan_start_timeFrom)
	AND	(_plan_start_timeTo is null OR plan_start_time <= _plan_start_timeTo)	
	AND (_plan_end_timeFrom is null OR plan_end_time >= _plan_end_timeFrom)
	AND	(_plan_end_timeTo is null OR plan_end_time <= _plan_end_timeTo)	
	AND (_real_start_tateFrom is null OR real_start_tate >= _real_start_tateFrom)
	AND	(_real_start_tateTo is null OR real_start_tate <= _real_start_tateTo)	
	AND (_real_end_timeFrom is null OR real_end_time >= _real_end_timeFrom)
	AND	(_real_end_timeTo is null OR real_end_time <= _real_end_timeTo)	
	AND (_need_approval is null OR need_approval = _need_approval)	
	AND (_is_event is null OR is_event = _is_event)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_rate is null OR rate = _rate)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_task_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_is_group	boolean DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_type	integer DEFAULT null,
	_id_phase	integer DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_plan_start_timeFrom	timestamp without time zone DEFAULT null,
	_plan_start_timeTo	timestamp without time zone DEFAULT null,
	_plan_end_timeFrom	timestamp without time zone DEFAULT null,
	_plan_end_timeTo	timestamp without time zone DEFAULT null,
	_real_start_tateFrom	timestamp without time zone DEFAULT null,
	_real_start_tateTo	timestamp without time zone DEFAULT null,
	_real_end_timeFrom	timestamp without time zone DEFAULT null,
	_real_end_timeTo	timestamp without time zone DEFAULT null,
	_need_approval	boolean DEFAULT null,
	_is_event	boolean DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_rate	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM task WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_is_group is null OR is_group = _is_group)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_id_phase is null OR id_phase = _id_phase)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_status is null OR status = _status)	
	AND (_plan_start_timeFrom is null OR plan_start_time >= _plan_start_timeFrom)
	AND	(_plan_start_timeTo is null OR plan_start_time <= _plan_start_timeTo)	
	AND (_plan_end_timeFrom is null OR plan_end_time >= _plan_end_timeFrom)
	AND	(_plan_end_timeTo is null OR plan_end_time <= _plan_end_timeTo)	
	AND (_real_start_tateFrom is null OR real_start_tate >= _real_start_tateFrom)
	AND	(_real_start_tateTo is null OR real_start_tate <= _real_start_tateTo)	
	AND (_real_end_timeFrom is null OR real_end_time >= _real_end_timeFrom)
	AND	(_real_end_timeTo is null OR real_end_time <= _real_end_timeTo)	
	AND (_need_approval is null OR need_approval = _need_approval)	
	AND (_is_event is null OR is_event = _is_event)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_rate is null OR rate = _rate)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_task_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM task
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_task_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_is_group boolean,
	_id_employee integer,
	_id_type integer,
	_id_phase integer,
	_id_parent integer,
	_status integer,
	_plan_start_time timestamp without time zone,
	_plan_end_time timestamp without time zone,
	_real_start_tate timestamp without time zone,
	_real_end_time timestamp without time zone,
	_need_approval boolean,
	_is_event boolean,
	_is_draft boolean,
	_notification integer,
	_rate integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE task 
	SET 		name = _name,
		code = _code,
		description = _description,
		is_group = _is_group,
		id_employee = _id_employee,
		id_type = _id_type,
		id_phase = _id_phase,
		id_parent = _id_parent,
		status = _status,
		plan_start_time = _plan_start_time,
		plan_end_time = _plan_end_time,
		real_start_tate = _real_start_tate,
		real_end_time = _real_end_time,
		need_approval = _need_approval,
		is_event = _is_event,
		is_draft = _is_draft,
		notification = _notification,
		rate = _rate,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of task

-- task_type
CREATE OR REPLACE FUNCTION sp_task_type_insert(
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_notification	integer DEFAULT null,
	_default_role	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO task_type(name,code,description,notification,default_role,creator,updator,deletion_time)
	VALUES(_name,_code,_description,_notification,_default_role,_creator,_creator,null);
RETURN currval('task_type_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_task_type_select(_id	integer)
RETURNS SETOF task_type
AS $$
BEGIN
	RETURN QUERY SELECT * FROM task_type WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_task_type_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_notification	integer DEFAULT null,
	_default_role	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF task_type
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM task_type WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_notification is null OR notification = _notification)	
	AND (_default_role is null OR default_role = _default_role)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_task_type_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_notification	integer DEFAULT null,
	_default_role	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM task_type WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_notification is null OR notification = _notification)	
	AND (_default_role is null OR default_role = _default_role)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_task_type_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM task_type
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_task_type_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_notification integer,
	_default_role integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE task_type 
	SET 		name = _name,
		code = _code,
		description = _description,
		notification = _notification,
		default_role = _default_role,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of task_type

-- unit
CREATE OR REPLACE FUNCTION sp_unit_insert(
	_name	character varying(255),
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_abbreviation	character varying(10) DEFAULT null,
	_foreign_name	character varying(255) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO unit(name,code,description,abbreviation,foreign_name,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_name,_code,_description,_abbreviation,_foreign_name,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('unit_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_unit_select(_id	integer)
RETURNS SETOF unit
AS $$
BEGIN
	RETURN QUERY SELECT * FROM unit WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_unit_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_abbreviation	character varying(10) DEFAULT null,
	_foreign_name	character varying(255) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF unit
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM unit WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_abbreviation is null OR abbreviation = _abbreviation)	
	AND (_foreign_name is null OR foreign_name = _foreign_name)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_unit_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_abbreviation	character varying(10) DEFAULT null,
	_foreign_name	character varying(255) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM unit WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_abbreviation is null OR abbreviation = _abbreviation)	
	AND (_foreign_name is null OR foreign_name = _foreign_name)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_unit_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM unit
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_unit_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_abbreviation character varying(10),
	_foreign_name character varying(255),
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE unit 
	SET 		name = _name,
		code = _code,
		description = _description,
		abbreviation = _abbreviation,
		foreign_name = _foreign_name,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of unit

-- venue
CREATE OR REPLACE FUNCTION sp_venue_insert(
	_id_place	integer DEFAULT null,
	_id_type	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_company	integer DEFAULT null,
	_abb_name	character varying(10) DEFAULT null,
	_short_name	character varying(25) DEFAULT null,
	_foreign_name	character varying(255) DEFAULT null,
	_address	character varying(255) DEFAULT null,
	_telephone	character varying(20) DEFAULT null,
	_fax	character varying(50) DEFAULT null,
	_capacity	integer DEFAULT null,
	_contact_person_1	character varying(255) DEFAULT null,
	_contact_phone_1	character varying(50) DEFAULT null,
	_contact_mobile_1	character varying(50) DEFAULT null,
	_contact_email_1	character varying(255) DEFAULT null,
	_contact_person_2	character varying(255) DEFAULT null,
	_contact_phone_2	character varying(50) DEFAULT null,
	_contact_mobile_2	character varying(50) DEFAULT null,
	_contact_email_2	character varying(255) DEFAULT null,
	_remark	character varying DEFAULT null,
	_map_object	oid DEFAULT null,
	_map_file	bytea DEFAULT null,
	_map_path	character varying(255) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO venue(id_place,id_type,name,code,description,id_company,abb_name,short_name,foreign_name,address,telephone,fax,capacity,contact_person_1,contact_phone_1,contact_mobile_1,contact_email_1,contact_person_2,contact_phone_2,contact_mobile_2,contact_email_2,remark,map_object,map_file,map_path,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_id_place,_id_type,_name,_code,_description,_id_company,_abb_name,_short_name,_foreign_name,_address,_telephone,_fax,_capacity,_contact_person_1,_contact_phone_1,_contact_mobile_1,_contact_email_1,_contact_person_2,_contact_phone_2,_contact_mobile_2,_contact_email_2,_remark,_map_object,_map_file,_map_path,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('venue_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_venue_select(_id	integer)
RETURNS SETOF venue
AS $$
BEGIN
	RETURN QUERY SELECT * FROM venue WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_venue_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_place	integer DEFAULT null,
	_id_type	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_company	integer DEFAULT null,
	_abb_name	character varying(10) DEFAULT null,
	_short_name	character varying(25) DEFAULT null,
	_foreign_name	character varying(255) DEFAULT null,
	_address	character varying(255) DEFAULT null,
	_telephone	character varying(20) DEFAULT null,
	_fax	character varying(50) DEFAULT null,
	_capacity	integer DEFAULT null,
	_contact_person_1	character varying(255) DEFAULT null,
	_contact_phone_1	character varying(50) DEFAULT null,
	_contact_mobile_1	character varying(50) DEFAULT null,
	_contact_email_1	character varying(255) DEFAULT null,
	_contact_person_2	character varying(255) DEFAULT null,
	_contact_phone_2	character varying(50) DEFAULT null,
	_contact_mobile_2	character varying(50) DEFAULT null,
	_contact_email_2	character varying(255) DEFAULT null,
	_remark	character varying DEFAULT null,
	_map_object	oid DEFAULT null,
	_map_file	bytea DEFAULT null,
	_map_path	character varying(255) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF venue
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM venue WHERE
	 	(_id is null OR id = _id)	
	AND (_id_place is null OR id_place = _id_place)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_abb_name is null OR abb_name = _abb_name)	
	AND (_short_name is null OR short_name = _short_name)	
	AND (_foreign_name is null OR foreign_name = _foreign_name)	
	AND (_address is null OR address = _address)	
	AND (_telephone is null OR telephone = _telephone)	
	AND (_fax is null OR fax = _fax)	
	AND (_capacity is null OR capacity = _capacity)	
	AND (_contact_person_1 is null OR contact_person_1 = _contact_person_1)	
	AND (_contact_phone_1 is null OR contact_phone_1 = _contact_phone_1)	
	AND (_contact_mobile_1 is null OR contact_mobile_1 = _contact_mobile_1)	
	AND (_contact_email_1 is null OR contact_email_1 = _contact_email_1)	
	AND (_contact_person_2 is null OR contact_person_2 = _contact_person_2)	
	AND (_contact_phone_2 is null OR contact_phone_2 = _contact_phone_2)	
	AND (_contact_mobile_2 is null OR contact_mobile_2 = _contact_mobile_2)	
	AND (_contact_email_2 is null OR contact_email_2 = _contact_email_2)	
	AND (_remark is null OR remark = _remark)	
	AND (_map_object is null OR map_object = _map_object)	
	AND (_map_file is null OR map_file = _map_file)	
	AND (_map_path is null OR map_path = _map_path)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_venue_count (
	_id	integer DEFAULT null,
	_id_place	integer DEFAULT null,
	_id_type	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_company	integer DEFAULT null,
	_abb_name	character varying(10) DEFAULT null,
	_short_name	character varying(25) DEFAULT null,
	_foreign_name	character varying(255) DEFAULT null,
	_address	character varying(255) DEFAULT null,
	_telephone	character varying(20) DEFAULT null,
	_fax	character varying(50) DEFAULT null,
	_capacity	integer DEFAULT null,
	_contact_person_1	character varying(255) DEFAULT null,
	_contact_phone_1	character varying(50) DEFAULT null,
	_contact_mobile_1	character varying(50) DEFAULT null,
	_contact_email_1	character varying(255) DEFAULT null,
	_contact_person_2	character varying(255) DEFAULT null,
	_contact_phone_2	character varying(50) DEFAULT null,
	_contact_mobile_2	character varying(50) DEFAULT null,
	_contact_email_2	character varying(255) DEFAULT null,
	_remark	character varying DEFAULT null,
	_map_object	oid DEFAULT null,
	_map_file	bytea DEFAULT null,
	_map_path	character varying(255) DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM venue WHERE
	 	(_id is null OR id = _id)	
	AND (_id_place is null OR id_place = _id_place)	
	AND (_id_type is null OR id_type = _id_type)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_company is null OR id_company = _id_company)	
	AND (_abb_name is null OR abb_name = _abb_name)	
	AND (_short_name is null OR short_name = _short_name)	
	AND (_foreign_name is null OR foreign_name = _foreign_name)	
	AND (_address is null OR address = _address)	
	AND (_telephone is null OR telephone = _telephone)	
	AND (_fax is null OR fax = _fax)	
	AND (_capacity is null OR capacity = _capacity)	
	AND (_contact_person_1 is null OR contact_person_1 = _contact_person_1)	
	AND (_contact_phone_1 is null OR contact_phone_1 = _contact_phone_1)	
	AND (_contact_mobile_1 is null OR contact_mobile_1 = _contact_mobile_1)	
	AND (_contact_email_1 is null OR contact_email_1 = _contact_email_1)	
	AND (_contact_person_2 is null OR contact_person_2 = _contact_person_2)	
	AND (_contact_phone_2 is null OR contact_phone_2 = _contact_phone_2)	
	AND (_contact_mobile_2 is null OR contact_mobile_2 = _contact_mobile_2)	
	AND (_contact_email_2 is null OR contact_email_2 = _contact_email_2)	
	AND (_remark is null OR remark = _remark)	
	AND (_map_object is null OR map_object = _map_object)	
	AND (_map_file is null OR map_file = _map_file)	
	AND (_map_path is null OR map_path = _map_path)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_venue_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM venue
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_venue_update 
(	_id integer,
	_id_place integer,
	_id_type integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_id_company integer,
	_abb_name character varying(10),
	_short_name character varying(25),
	_foreign_name character varying(255),
	_address character varying(255),
	_telephone character varying(20),
	_fax character varying(50),
	_capacity integer,
	_contact_person_1 character varying(255),
	_contact_phone_1 character varying(50),
	_contact_mobile_1 character varying(50),
	_contact_email_1 character varying(255),
	_contact_person_2 character varying(255),
	_contact_phone_2 character varying(50),
	_contact_mobile_2 character varying(50),
	_contact_email_2 character varying(255),
	_remark character varying,
	_map_object oid,
	_map_file bytea,
	_map_path character varying(255),
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE venue 
	SET 		id_place = _id_place,
		id_type = _id_type,
		name = _name,
		code = _code,
		description = _description,
		id_company = _id_company,
		abb_name = _abb_name,
		short_name = _short_name,
		foreign_name = _foreign_name,
		address = _address,
		telephone = _telephone,
		fax = _fax,
		capacity = _capacity,
		contact_person_1 = _contact_person_1,
		contact_phone_1 = _contact_phone_1,
		contact_mobile_1 = _contact_mobile_1,
		contact_email_1 = _contact_email_1,
		contact_person_2 = _contact_person_2,
		contact_phone_2 = _contact_phone_2,
		contact_mobile_2 = _contact_mobile_2,
		contact_email_2 = _contact_email_2,
		remark = _remark,
		map_object = _map_object,
		map_file = _map_file,
		map_path = _map_path,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;

-- end of venue

-- venue_type
CREATE OR REPLACE FUNCTION sp_venue_type_insert(
	_name	character varying(255),
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO venue_type(name,code,description,id_parent,status,is_draft,notification,creator,updator,deletion_time)
	VALUES(_name,_code,_description,_id_parent,_status,_is_draft,_notification,_creator,_creator,null);
RETURN currval('venue_type_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_venue_type_select(_id	integer)
RETURNS SETOF venue_type
AS $$
BEGIN
	RETURN QUERY SELECT * FROM venue_type WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_venue_type_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF venue_type
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM venue_type WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_venue_type_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying DEFAULT null,
	_id_parent	integer DEFAULT null,
	_status	integer DEFAULT null,
	_is_draft	boolean DEFAULT null,
	_notification	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM venue_type WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_id_parent is null OR id_parent = _id_parent)	
	AND (_status is null OR status = _status)	
	AND (_is_draft is null OR is_draft = _is_draft)	
	AND (_notification is null OR notification = _notification)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_venue_type_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM venue_type
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_venue_type_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying,
	_id_parent integer,
	_status integer,
	_is_draft boolean,
	_notification integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE venue_type 
	SET 		name = _name,
		code = _code,
		description = _description,
		id_parent = _id_parent,
		status = _status,
		is_draft = _is_draft,
		notification = _notification,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;
-- end of venue_type

-- project_role
CREATE OR REPLACE FUNCTION sp_project_role_insert(
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying(255) DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO project_role(name,code,description,creator,updator,deletion_time)
	VALUES(_name,_code,_description,_creator,_creator,null);
RETURN currval('project_role_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_project_role_select(_id	integer)
RETURNS SETOF project_role
AS $$
BEGIN
	RETURN QUERY SELECT * FROM project_role WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_project_role_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying(255) DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF project_role
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM project_role WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_project_role_count (
	_id	integer DEFAULT null,
	_name	character varying(255) DEFAULT null,
	_code	character varying(20) DEFAULT null,
	_description	character varying(255) DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM project_role WHERE
	 	(_id is null OR id = _id)	
	AND (_name is null OR name = _name)	
	AND (_code is null OR code = _code)	
	AND (_description is null OR description = _description)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION sp_project_role_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM project_role
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION sp_project_role_update 
(	_id integer,
	_name character varying(255),
	_code character varying(20),
	_description character varying(255),
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE project_role 
	SET 		name = _name,
		code = _code,
		description = _description,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;
-- end of project_role

-- team_allocation
CREATE OR REPLACE FUNCTION sp_team_allocation_insert(
	_id_project	integer DEFAULT null,
	_id_phase	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_project_role	integer DEFAULT null,
	_man_day	integer DEFAULT null,
	_description	character varying(255) DEFAULT null,
	_remark	character varying(255) DEFAULT null,
	_status	integer DEFAULT null,
	_creator	integer DEFAULT null
)RETURNS int AS $$
BEGIN
	INSERT INTO team_allocation(id_project,id_phase,id_employee,id_project_role,man_day,description,remark,status,creator,updator,deletion_time)
	VALUES(_id_project,_id_phase,_id_employee,_id_project_role,_man_day,_description,_remark,_status,_creator,_creator,null);
RETURN currval('team_allocation_id_seq'::regclass);
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_team_allocation_select(_id	integer)
RETURNS SETOF team_allocation
AS $$
BEGIN
	RETURN QUERY SELECT * FROM team_allocation WHERE id = _id;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_team_allocation_selectList (
	_limit	integer,
	_offset integer,
	_id	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_id_phase	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_project_role	integer DEFAULT null,
	_man_day	integer DEFAULT null,
	_description	character varying(255) DEFAULT null,
	_remark	character varying(255) DEFAULT null,
	_status	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS SETOF team_allocation
AS $$
BEGIN
	IF _limit IS NULL THEN
		_limit = 2147483647;
	END IF;
	IF _offset IS NULL THEN
		_offset = 0;
	END IF;
RETURN QUERY SELECT * FROM team_allocation WHERE
	 	(_id is null OR id = _id)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_id_phase is null OR id_phase = _id_phase)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_project_role is null OR id_project_role = _id_project_role)	
	AND (_man_day is null OR man_day = _man_day)	
	AND (_description is null OR description = _description)	
	AND (_remark is null OR remark = _remark)	
	AND (_status is null OR status = _status)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo)	ORDER BY updated DESC  LIMIT _limit OFFSET _offset;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_team_allocation_count (
	_id	integer DEFAULT null,
	_id_project	integer DEFAULT null,
	_id_phase	integer DEFAULT null,
	_id_employee	integer DEFAULT null,
	_id_project_role	integer DEFAULT null,
	_man_day	integer DEFAULT null,
	_description	character varying(255) DEFAULT null,
	_remark	character varying(255) DEFAULT null,
	_status	integer DEFAULT null,
	_createdFrom	timestamp without time zone DEFAULT null,
	_createdTo	timestamp without time zone DEFAULT null,
	_creator	integer DEFAULT null,
	_updatedFrom	timestamp without time zone DEFAULT null,
	_updatedTo	timestamp without time zone DEFAULT null,
	_updator	integer DEFAULT null,
	_deleted	boolean DEFAULT null,
	_deletion_timeFrom	timestamp without time zone DEFAULT null,
	_deletion_timeTo	timestamp without time zone DEFAULT null
)
RETURNS int AS $$
BEGIN
	RETURN COUNT(*) FROM team_allocation WHERE
	 	(_id is null OR id = _id)	
	AND (_id_project is null OR id_project = _id_project)	
	AND (_id_phase is null OR id_phase = _id_phase)	
	AND (_id_employee is null OR id_employee = _id_employee)	
	AND (_id_project_role is null OR id_project_role = _id_project_role)	
	AND (_man_day is null OR man_day = _man_day)	
	AND (_description is null OR description = _description)	
	AND (_remark is null OR remark = _remark)	
	AND (_status is null OR status = _status)	
	AND (_createdFrom is null OR created >= _createdFrom)
	AND	(_createdTo is null OR created <= _createdTo)	
	AND (_creator is null OR creator = _creator)	
	AND (_updatedFrom is null OR updated >= _updatedFrom)
	AND	(_updatedTo is null OR updated <= _updatedTo)	
	AND (_updator is null OR updator = _updator)	
	AND (_deleted is null OR deleted = _deleted)	
	AND (_deletion_timeFrom is null OR deletion_time >= _deletion_timeFrom)
	AND	(_deletion_timeTo is null OR deletion_time <= _deletion_timeTo);
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION sp_team_allocation_delete(_id integer)
	RETURNS INT as
$$
BEGIN
		DELETE FROM team_allocation
 		WHERE "id" = _id;
		RETURN 0;
END;
$$ LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION sp_team_allocation_update 
(	_id integer,
	_id_project integer,
	_id_phase integer,
	_id_employee integer,
	_id_project_role integer,
	_man_day integer,
	_description character varying(255),
	_remark character varying(255),
	_status integer,
	_updator integer,
	_deleted boolean,
	_deletion_time timestamp without time zone  ) 
RETURNS INT AS 
$$
BEGIN 
	UPDATE team_allocation 
	SET 		id_project = _id_project,
		id_phase = _id_phase,
		id_employee = _id_employee,
		id_project_role = _id_project_role,
		man_day = _man_day,
		description = _description,
		remark = _remark,
		status = _status,
		updated = now(),
		updator = _updator,
		deleted = _deleted,
		deletion_time = _deletion_time
 	WHERE id = _id;
	RETURN 0;
END; 
$$ LANGUAGE plpgsql;
-- end of team_allocation